[英]NullPointerException from Canvas class(J2ME)
i'm having trouble from fetching data from my TimerCan class. 我无法从TimerCan类中获取数据。 like String and integers. 像字符串和整数。
i have a method(static method) for getting this data and set it to another class. 我有一个方法(静态方法)来获取此数据并将其设置为另一个类。 but every time i run my program i always get NULLPOINTEREXCEPTION in line.. 但是每次我运行程序时,我总会得到NULLPOINTEREXCEPTION信息。
if(opp.equalsIgnoreCase("incrementing")){///heree
startTimer();
if(hour[current]==hhh&&min[current]==mmm&&sec[current]==sss)
{
this.start = false;
this.stop = true;
this.timer.cancel();
}
else if(opp.equalsIgnoreCase("decrementing")){
startTimerD();
if(hour[current]==0&&min[current]==0&&sec[current]==0)
{
this.start = false;
this.stop = true;
this.timer.cancel();
and heres the full code my TimerCan class 这是我的TimerCan类的完整代码
public class TimerCan extends Canvas
{
private Timer timer;
private Midlet myMid;
private Player z;
private int hour[],sec[],min[],msec[],maxX,maxY,current,length,x,y;
private String time,opp;
private int strWid,hhh,mmm,sss;
private int strht;
private boolean start;
private boolean stop;
public Image img;
Data data;
public TimerCan(Midlet midlet)
{
this.myMid= midlet;
data = new Data();
opp=data.getData();
hhh=data.getDatah();
mmm=data.getDatam();
sss=data.getDatas();
try
{
this.maxX = this.getWidth();
this.maxY = this.getHeight();
this.current = 0;
this.hour = new int[30];
this.min = new int[30];
this.sec = new int[30];
this.msec = new int[30];
this.start = false;
this.stop = true;
for(int j = 0 ; j <= 30 ; j++)
{
this.hour[j] = 0;
this.min[j] = 0;
this.sec[j] = 0;
this.msec[j] = 0;
}
}catch(Exception e)
{}
}
public void paint(Graphics g)
{
Font font = Font.getFont(0,1,16);
this.strWid = font.stringWidth("this.time");
this.strht = font.getHeight();
if(hour[current] < 10)
{
time = "0"+String.valueOf(this.hour[current])+":";
}
else
{
time = String.valueOf(this.hour[current]) + ":";
}
if(min[current] < 10)
{
time = time+"0"+String.valueOf(this.min[current]) + ":";
}
else
{
time = time+String.valueOf(this.min[current]) + ":";
}
if(sec[current] < 10)
{
time = time+"0"+String.valueOf(this.sec[current]) + ":";
}
else
{
time = time + String.valueOf(this.sec[current]) + ":";
}
if(msec[current] < 10)
{
time = time+"0"+String.valueOf(this.msec[current]);
}
else
{
time = time+String.valueOf(this.msec[current]);
}
this.strWid = font.stringWidth(time);
this.length = this.maxX - this.strWid;
this.length /= 2;
try{
img = Image.createImage("/picture/aa.png");
}
catch(Exception error){
}
x = this.getWidth()/2;
y = this.getHeight()/2;
g.setColor(63,155,191);
g.fillRect(0,0,maxX, maxY);
g.drawImage(img, x, y, Graphics.VCENTER | Graphics.HCENTER);
g.setColor(0,0,0) ;
g.drawString(time,length+15,150,Graphics.TOP|Graphics.LEFT);
}
private void startTimer()
{
TimerTask task = new TimerTask()
{
public void run()
{
msec[current]++ ;
if(msec[current] == 100)
{
msec[current] = 0 ;
sec[current]++ ;
}
else if(sec[current] ==60)
{
sec[current] = 0 ;
min[current]++ ;
}
else if(min[current] == 60)
{
min[current] = 0 ;
hour[current]++ ;
}
else if(hour[current] == 24)
{
hour[current] = 0 ;
}
repaint();
}
};
timer = new Timer();
timer.scheduleAtFixedRate(task,10,10) ;
}
private void startTimerD()
{
TimerTask task = new TimerTask()
{
public void run()
{
msec[current]-- ;
if(msec[current] == 0)
{
msec[current] = 100 ;
sec[current]-- ;
}
else if(sec[current] ==0)
{
sec[current] = sss;
min[current]--;
}
else if(min[current] == 0)
{
min[current] =mmm;
hour[current]--;
}
else if(hour[current] == 0)
{
hour[current] = hhh;
}
repaint();
}
};
timer = new Timer();
timer.scheduleAtFixedRate(task,10,10) ;
}
protected void keyPressed(int keyCode)
{
if(keyCode == Canvas.KEY_NUM1)
{
if(this.start == false)
{
this.start=true;
this.stop=false;
}
else if(this.stop == false)
{
this.start = false ;
this.stop = true ;
this.timer.cancel();
}
if(start == true)
{
check();
}
}
if(keyCode == Canvas.KEY_NUM2)
{
this.min[current]=0;
this.sec[current]=0;
this.msec[current]=0;
this.start = false;
this.stop = true;
this.timer.cancel();
try{
z.deallocate();
}
catch(Exception e){}
repaint();
}
if(keyCode == Canvas.KEY_NUM3)
{
if(this.stop == false)
{
this.start = false;
this.stop = true;
this.timer.cancel();
try{
InputStream inss = getClass().getResourceAsStream("alarm.wav");
InputStreamReader iis= new InputStreamReader(inss);
z = Manager.createPlayer(inss,"audio/x-wav");
z.prefetch();
z.setLoopCount(2);
z.start();
}
catch(Exception e){
}
}
}
if(keyCode==Canvas.KEY_NUM0)
{
try{
z.deallocate();
}
catch(Exception e){}
myMid.exit();
}
}
public void check()
{
if(opp.equalsIgnoreCase("incrementing")){
startTimer();
if(hour[current]==hhh&&min[current]==mmm&&sec[current]==sss)
{
this.start = false;
this.stop = true;
this.timer.cancel();
try{
InputStream inss = getClass().getResourceAsStream("alarm.wav");
InputStreamReader iis= new InputStreamReader(inss);
z = Manager.createPlayer(inss,"audio/x-wav");
z.prefetch();
z.setLoopCount(2);
z.start();
}
catch(Exception e){
}
}
}
else if(opp.equalsIgnoreCase("decrementing")){
startTimerD();
if(hour[current]==0&&min[current]==0&&sec[current]==0)
{
this.start = false;
this.stop = true;
this.timer.cancel();
try{
InputStream inss = getClass().getResourceAsStream("alarm.wav");
InputStreamReader iis= new InputStreamReader(inss);
z = Manager.createPlayer(inss,"audio/x-wav");
z.prefetch();
z.setLoopCount(2);
z.start();
}
catch(Exception e){
}
}
and heres the Data classs 这是数据类
public class Data{ 公开课数据{
String nameD;
int hhD;
int mmD;
int ssD;
public void setData(String name){
this.nameD = name;
}
public String getData(){
return this.nameD;
}
//hour
public void setDatah(int hhh){
this.hhD = hhh;
}
public int getDatah(){
return this.hhD;
}
//
public void setDatam(int hhh){
this.mmD = hhh;
}
public int getDatam(){
return this.mmD;
}
//
public void setDatas(int sss){
this.ssD = sss;
}
public int getDatas(){
return this.ssD;
}
} }
This happens because you have not initialized variable data
, so it remains null
. 发生这种情况是因为尚未初始化变量data
,因此它保持为null
。 This is the obvious reason of NullPointerException
. 这是NullPointerException
的明显原因。
You have to call data = new Data();
您必须调用data = new Data();
prior using it. 使用之前。
You wrote 你写了
data = new Data();
in your TimerCan
class. 在您的TimerCan
类中。 This made a Data
object that didn't have nameD
set to anything; 这使得没有将nameD
设置为任何值的Data
对象; so when you wrote 所以当你写
opp = data.getData()
later, you were setting opp
to null
- that is, there wasn't actually an object referenced by opp
. 后来,您将opp
设置为null
,也就是说,实际上没有opp
引用的对象。 When you wrote 当你写
if (opp.equalsIgnoreCase("incrementing"))
this caused a problem. 这引起了问题。 You can't call a method, when the object that you're trying to call it on is absent. 当您要在其上调用的对象不存在时,您将无法调用方法。
If it's legitimate for opp
to be null, but you still have code that you want to run when opp
is "incrementing"
you can write it like this. 如果opp
为null是合法的,但是在opp
"incrementing"
时您仍然有要运行的代码,则可以这样编写。
if (opp != null && opp.equalsIgnoreCase("incrementing"))
which will check whether opp
is null before it calls the equalsIgnoreCase
method - and you won't get the NullPointerException
. 它将在调用equalsIgnoreCase
方法之前检查opp
是否为null ,并且您不会获得NullPointerException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.