【问题标题】:NullPointerException from Canvas class(J2ME)来自 Canvas 类 (J2ME) 的 NullPointerException
【发布时间】:2014-02-18 12:40:40
【问题描述】:

我无法从我的 TimerCan 类中获取数据。比如字符串和整数。

我有一个方法(静态方法)来获取这些数据并将其设置为另一个类。但是每次我运行我的程序时,我总是会得到 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();

这是我的 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){
    }
        }

这里是数据类

公共类数据{

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; 
}

}

【问题讨论】:

  • 你的Data 类有构造函数吗?如果是这样,它是否设置了getData()getDatah()getDatam()getDatas() 返回的字段的值?如果这两种情况都不是,那么opp, hhh, mmm, sss 将全部为空,并且每当您尝试使用其中任何一种时,都会收到空指针异常。
  • 我该怎么做??我对此有点困惑......

标签: java arrays timer java-me midp


【解决方案1】:

你写的

data = new Data();

在您的TimerCan 班级中。这使得Data 对象没有设置任何nameD;所以当你写

opp = data.getData()

稍后,您将opp 设置为null - 也就是说,opp 实际上没有引用对象。当你写了

if (opp.equalsIgnoreCase("incrementing"))

这导致了一个问题。当您尝试调用的对象不存在时,您无法调用该方法。

如果opp 为空是合法的,但当opp"incrementing" 时,您仍然有要运行的代码,您可以这样编写。

if (opp != null && opp.equalsIgnoreCase("incrementing"))

它将检查opp 是否为空它调用equalsIgnoreCase 方法之前 - 你不会得到NullPointerException

【讨论】:

  • 感谢您的回复。现在为了使用来自另一个类的数据,首先检查它是否等于 null...
  • 还有一个NULLpointer =__=,为什么是null?在这一行.....this.timer.cancel();
  • 嗯,这通常表明您在该行运行时没有将timer 设置为任何值。说真的,我强烈建议您使用调试器来解决这类问题。
  • 但我不知道如何调试。
  • 很公平。我希望这听起来不是傲慢,但我认为学习如何做对你来说非常重要。调试器是任何程序员了解如何使用的重要工具,并且调试器的使用应该包含在每个编程课程的开头附近。不会使用调试器的程序员就像不会使用锤子的建造者。
【解决方案2】:

这是因为你没有初始化变量data,所以它仍然是null。这就是NullPointerException 的明显原因。

您必须在使用前致电data = new Data();

【讨论】:

  • at if(opp.equalsIgnoreCase("incrementing")){
  • 这意味着你的opp也没有初始化。
  • 你的意思是,,,,这个? opp=data.getData();
猜你喜欢
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多