簡體   English   中英

來自Canvas類(J2ME)的NullPointerException

[英]NullPointerException from Canvas class(J2ME)

我無法從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 ,因此它保持為null 這是NullPointerException的明顯原因。

您必須調用data = new Data(); 使用之前。

你寫了

data = new Data();

在您的TimerCan類中。 這使得沒有將nameD設置為任何值的Data對象; 所以當你寫

opp = data.getData()

后來,您將opp設置為null ,也就是說,實際上沒有opp引用的對象。 當你寫

if (opp.equalsIgnoreCase("incrementing"))

這引起了問題。 當您要在其上調用的對象不存在時,您將無法調用方法。

如果opp為null是合法的,但是在opp "incrementing"時您仍然有要運行的代碼,則可以這樣編寫。

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

它將調用equalsIgnoreCase方法之前檢查opp是否為null 並且您不會獲得NullPointerException

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM