刚开始学java就遇到一个问题,各位帮忙看看了,欧真苯!

zhanchifeixiang 2002-03-01 11:38:03
我用一个list控件,现想对他进行操作,但老是不成功,提示null pointer exception!
不知道什么地方错误,我创建了对象了。
源码如下:
import java.awt.*;

public class order extends java.applet.Applet
{
public void init()
{ List ProductList=new List(4,false);
ProductList.addItem("Oscar");
ProductList.addItem("Lionhead");
ProductList.addItem("Jack Dempsey");
ProductList.addItem("Angelfish");

add(ProductList);
......
......
resetValues();
}

public void resetValues(){
SubTotalLabel.setText("$0.0");
TotalLabel.setText("$0.0");
AmountLabel.setText("$0.0");
PricePerItemLabel.setText("$0.0");

ProductList.select(1); **xxxxxxxxxxxxxxxxxxxxxx**
SizeChoice.select(1);
OrderAmountSlider.setValue(0);

NameEntryField.setText("");
StreetEntryField.setText("");
CityEntryField.setText("");
ZipEntryField.setText("");
}

public boolean handleEvent(Event InEvent)
{
if(InEvent.id==Event.SCROLL_LINE_UP||InEvent.id==Event.SCROLL_LINE_DOWN){
updateValues();
}else
if(InEvent.target==ProductList){
updateValues();
}else
if(InEvent.target==ClearButton){
resetValues();
}else
if(InEvent.target==QuitButton){
//Alabel.setText("Quit Button");
}else
if(InEvent.target==SubmitButton){
//Alabel.setText("Submit Button");
}
return super.handleEvent(InEvent);
}

public boolean action(Event InEvent,Object SomeObject){
if(InEvent.target==SizeChoice){
updateValues();
return true;
}else
if(InEvent.target==NameEntryField){
return true;
}else
return false;
}

private void updateValues(){
。。。。。。。
。。。。。。。
}
}

打杈的地方报错!
...全文
29 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhanchifeixiang 2002-03-04
  • 打赏
  • 举报
回复
rpw(rpw)兄说得很正确,按照他说得我重新改了一下程序,运行完全正确。看来一些东西不能随便添加的。
------------------------------------
谢谢各位的帮助,谢谢!
coldjava 2002-03-01
  • 打赏
  • 举报
回复
看不懂,怎么没接口?
zhanchifeixiang 2002-03-01
  • 打赏
  • 举报
回复
peacock_king(孔雀王) :
错误是在页面开始加载applet时就报错的,时间处理还没有开始。我觉得可能不是这个问题。
peacock_king 2002-03-01
  • 打赏
  • 举报
回复
好像是jdk1.0的事件处理模型的兼容问题(action())
怎么不用新事件模型?
就是somecomponent.add***listener()
处理事件用对应的方法。

zhanchifeixiang 2002-03-01
  • 打赏
  • 举报
回复
不是,我是想在此处将list初始化,设置为1。
而且,不至list出错,choice等都是这个错误,不明白为什么
GJA106 2002-03-01
  • 打赏
  • 举报
回复
getSelectedIndexes是当你把List设成setMultipleMode(true)时用(允许一次选择多项);如果设成setMultipleMode(false)则用getSelectedIndex()就可以了。
GJA106 2002-03-01
  • 打赏
  • 举报
回复
用ProductList.getItem(ProductList.getSelectedIndexes());
zhanchifeixiang 2002-03-01
  • 打赏
  • 举报
回复
补充一下,不至list出错,只要一牵扯控件事件的,都报这个错误。各位老兄帮帮忙呀
rpw 2002-03-01
  • 打赏
  • 举报
回复
我看是你的程序错了,你已经声明了一个私有的全局变量ProductList:
private List ProductList;

然后在init方法理由声明了一个同名的ProductList对象:
List ProductList=new List(4,false);
ProductList.addItem("Oscar");
ProductList.addItem("Lionhead");
ProductList.addItem("Jack Dempsey");
ProductList.addItem("Angelfish");
ProductList.select(1);
add(ProductList);
那么当init方法结束后,Applet的面板上应该有一个List对象,就是你看到的那个,但这个对象不是你声明的私有变量ProductList, 这时这个ProductList应该是null,那么在以后的其他方法中如果用到这个变量就会抛出空指针异常。

(我没有实际运行你的程序,也许会有不准确的地方)
zhanchifeixiang 2002-03-01
  • 打赏
  • 举报
回复
知道了,谢谢了
GJA106 2002-03-01
  • 打赏
  • 举报
回复
在第几行出现的null pointer exception。
不用下载api,你用当前包新的api代替就行了。因为在当前包中有新的api,所以系统才提示你就用新的api代替旧的api。
如果上面所说的addItem已经不用,用add。你看一下addItem是不是已经用横线划掉了。
GJA106 2002-03-01
  • 打赏
  • 举报
回复
在第几行出现的null pointer exception
zhanchifeixiang 2002-03-01
  • 打赏
  • 举报
回复
那么,哥们,我能够更新我的API吗?到那里下载?
GJA106 2002-03-01
  • 打赏
  • 举报
回复
Note: xxx.java uses or overrides a deprecated API.
Note:Recompile with -deprecation for details
是用了过时的api对当前程序没有影响。
List的addItem已经不用,用add。
你可以javac -deprecation e:\\order.java来看你用了多少个过时的api。
zhanchifeixiang 2002-03-01
  • 打赏
  • 举报
回复
我估计是API的问题,是我的不全,还是其他原因呢?
请求帮助!!!!!!!!!!!!!!
zhanchifeixiang 2002-03-01
  • 打赏
  • 举报
回复
GJA106(中文字符):
哦,怪了。那么你运行时,那么组件能够正常使用吗?尤其是scroll,我这边一滚动就出上述错误。
是不是我的环境有问题????????????
我安装的是jdk1.3.1_02,其他什么都设好了。是类包吗?
zhanchifeixiang 2002-03-01
  • 打赏
  • 举报
回复
对了,忘记说了:我在编译源文件时,总是提示
Note: xxx.java uses or overrides a deprecated API.
Note:Recompile with -deprecation for details
GJA106 2002-03-01
  • 打赏
  • 举报
回复
在我这儿运行没有出现什么异常,而且ProductList也成功自动指到第二项,applet中各个控件也可以正常操作。
zhanchifeixiang 2002-03-01
  • 打赏
  • 举报
回复
GJA106(中文字符):
直接添加select(1)是成功的。事件包(import java.awt.event.*;)也包进去了亚,还是出错。我用的是jdk1.3.1_02编译的
源代码给贴上把:
---------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;

public class order extends java.applet.Applet
{
private double SubTotalOne=0.0;
private double Total=0.0;

private double multiplierSmall=0.5;
private double multiplierMedium=1.0;
private double multiplierLarge=1.5;
private double multiplierJumbo=2.25;

private double ProdOneBaseValue=1.0;
private double ProdTwoBaseValue=1.33;
private double ProdThreeBaseValue=1.75;
private double ProdFourBaseValue=8.75;

private Button SubmitButton;
private Button ClearButton;
private Button QuitButton;
private Checkbox ReapeatCustCheckBox;
private Checkbox MailBox;
private Checkbox EmailBox;
private Checkbox PhoneBox;
private List ProductList;
private Choice SizeChoice;
private Scrollbar OrderAmountSlider;

private Label SubTotalLabel=new Label("$0.0");
private Label TotalLabel=new Label("$0.0");
private Label AmountLabel=new Label("$0.0");
private Label PricePerItemLabel=new Label("$0.0");

private TextField NameEntryField=new TextField(25);
private TextField ZipEntryField=new TextField(5);
private TextField StreetEntryField=new TextField(25);
private TextField CityEntryField=new TextField(25);

private TextArea CommentTextArea=new TextArea(4,25);

public void init()
{
Button SubmitButton=new Button("Submit");
add(SubmitButton);

Button ClearButton=new Button("Clear");
add(ClearButton);

Button QuitButton=new Button("Quit");
add(QuitButton);

Checkbox RepeatCustCheckBox=new Checkbox("Repeat Customer?");
add(RepeatCustCheckBox);

add(new Label("How would you like to be contacted?"));

CheckboxGroup ContactMethodGroup;
ContactMethodGroup=new CheckboxGroup();

Checkbox EmailBox=new Checkbox("Email",ContactMethodGroup,true);
Checkbox PhoneBox=new Checkbox("Phone",ContactMethodGroup,false);
Checkbox MailBox=new Checkbox("usMail",ContactMethodGroup,false);

add(EmailBox);
add(PhoneBox);
add(MailBox);

add(new Label("Products"));

List ProductList=new List(4,false);
ProductList.addItem("Oscar");
ProductList.addItem("Lionhead");
ProductList.addItem("Jack Dempsey");
ProductList.addItem("Angelfish");
ProductList.select(1);

add(ProductList);

add(new Label("Size:"));

Choice SizeChoice=new Choice();
SizeChoice.addItem("Jumbo");
SizeChoice.addItem("Large");
SizeChoice.addItem("Medium");
SizeChoice.addItem("Small");

add(SizeChoice);

add(new Label("Amount:"));

Scrollbar OrderAmountSlider=new Scrollbar(Scrollbar.VERTICAL,0,0,0,144);
add(OrderAmountSlider);

add(AmountLabel);
add(new Label("Subtotal:"));
add(SubTotalLabel);

add(new Label("Total:"));
add(TotalLabel);

add(new Label("Your information:"));
add(new Label("Name:"));
add(NameEntryField);
add(new Label("Street:"));
add(StreetEntryField);
add(new Label("City:"));
add(CityEntryField);
add(new Label("Zip:"));
add(ZipEntryField);
add(new Label("Comments:"));
add(CommentTextArea);

//resetValues();
}

public void resetValues(){
SubTotalLabel.setText("$0.0");
TotalLabel.setText("$0.0");
AmountLabel.setText("$0.0");
PricePerItemLabel.setText("$0.0");

ProductList.select(0);
SizeChoice.select(0);
OrderAmountSlider.setValue(0);

NameEntryField.setText("");
StreetEntryField.setText("");
CityEntryField.setText("");
ZipEntryField.setText("");
}

public boolean handleEvent(Event InEvent)
{
if(InEvent.id==Event.SCROLL_LINE_UP||InEvent.id==Event.SCROLL_LINE_DOWN){
updateValues();
}else
if(InEvent.target==ProductList){
updateValues();
}else
if(InEvent.target==ClearButton){
resetValues();
}else
if(InEvent.target==QuitButton){
//Alabel.setText("Quit Button");
}else
if(InEvent.target==SubmitButton){
//Alabel.setText("Submit Button");
}
return super.handleEvent(InEvent);
}

public boolean action(Event InEvent,Object SomeObject){
if(InEvent.target==SizeChoice){
updateValues();
return true;
}else
if(InEvent.target==NameEntryField){
return true;
}else
return false;
}

private void updateValues(){
int WhichChoice=SizeChoice.getSelectedIndex();
int AmountSelected=OrderAmountSlider.getValue();
int WhichProduct=ProductList.getSelectedIndex();

double CurrentBasePrice=0.0;
double CurrentModifier=0.0;

switch(WhichProduct){
case 0:
CurrentBasePrice=ProdOneBaseValue;
break;
case 1:
CurrentBasePrice=ProdTwoBaseValue;
break;
case 2:
CurrentBasePrice=ProdThreeBaseValue;
break;
case 3:
CurrentBasePrice=ProdFourBaseValue;
break;
}

switch(WhichChoice){
case 3:
CurrentModifier=multiplierSmall;
break;
case 2:
CurrentModifier=multiplierMedium;
break;
case 1:
CurrentModifier=multiplierLarge;
break;
case 0:
CurrentModifier=multiplierJumbo;
break;
}

AmountLabel.setText(Integer.toString(AmountSelected)+"");
double PricePerItem=(CurrentBasePrice * CurrentModifier);
PricePerItemLabel.setText(Double.toString(PricePerItem)+"");
double SubTotal=(CurrentBasePrice+CurrentModifier*AmountSelected);
SubTotalLabel.setText(Double.toString(SubTotal)+"");

TotalLabel.setText(SubTotalLabel.getText());

}
}
--------------------------------------------------------------------



GJA106 2002-03-01
  • 打赏
  • 举报
回复
没有产生什么异常。你是用什么编译的。
封住不必要的代码。
直接在
List ProductList=new List(4,false);
ProductList.addItem("Oscar");
ProductList.addItem("Lionhead");
ProductList.addItem("Jack Dempsey");
ProductList.addItem("Angelfish");
后添加select(1)看成不成功。
你把事件包包进去了没有:import java.awt.event.*;

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧