0

所以我写了一个程序,有 3 个 NPE - 我不明白为什么,也许我因流感而失明;)有人能告诉我出了什么问题吗?

我不粘贴整个代码,而只粘贴重要的类。其余的还可以。

package maszyna;
import java.util.*;

public class Main {


public static void main(String[] args) {

    User user = new User(); // first NPE
    Mode.start = true;
    Mode.pause = false; 

    Machine machine = new Machine();
    user.addListener(machine);       


    user.setTimeStart("1:15:30");
    //System.out.println("It's " +user.getTimeStart()); // but I can see this
    user.setTimePause("1:15:40");//when it's uncommented ofc
    user.setTimeResume("1:15:50");
    user.setTimeRestart("1:16:00");
    user.setTimeStop("1:16:10");



}
}

这是用户类:

package maszyna;
import static java.lang.Thread.sleep;
import java.util.*;



public class User{

    String name;
    String timeStart;
    String timeStop;
    String timePause;
    String timeResume;
    String timeRestart;
    String[] temp = new String[3];    
    int[] SMTi = new int[3];
    private List<UserListener> listeners;

           Thread life = new Thread(){
            @Override
    public void run()
    {
        while(Mode.start==true){

                        Calendar cal = Calendar.getInstance();     
                        int hour = cal.get(Calendar.HOUR_OF_DAY);
                        int minute = cal.get(Calendar.MINUTE);
                        int second = cal.get(Calendar.SECOND);

            machineStart(hour, minute, second);
            machineStop(hour, minute, second);
            machinePause(hour, minute, second);
                            machineResume(hour, minute, second);
                            machineRestart(hour, minute, second);

            try {
                sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

};

    public User(){    
      this.listeners = new ArrayList<UserListener>();
    life.start();
    }   



public String getTimeStart() {
    return timeStart;
}

public void setTimeStart(String timeStart) {
    this.timeStart = timeStart;
}

public String getTimeStop() {
    return timeStop;
}

public void setTimeStop(String timeStop) {
    this.timeStop = timeStop;
}

public String getTimePause() {
    return timePause;
}

public void setTimePause(String timePause) {
    this.timePause = timePause;
}

public String getTimeResume() {
    return timeResume;
}

public void setTimeResume(String timeResume) {
    this.timeResume = timeResume;
}

public String getTimeRestart() {
    return timeRestart;
}

public void setTimeRestart(String timeRestart) {
    this.timeRestart = timeRestart;
}

    int[] tStart = splitMyTime(getTimeStart()); //they are set, aren't they?
    int[] tPause = splitMyTime(getTimePause()); //I can get them in Main when I want to
    int[] tResume = splitMyTime(getTimeResume());
    int[] tRestart = splitMyTime(getTimeRestart());
    int[] tStop = splitMyTime(getTimeStop());



public int[] splitMyTime(String cos){

    if(cos==null){ //I made this if for tests
        System.out.println("OMG, it's null"); //it displays OMG 5 times
        int[] kot = {1, 2, 3};//and part below throws NPE, when if is hidden in comment
        Mode.start = false;
        return kot;
    }
    else{ // and this part causes NPE
    temp = cos.split(":|.");    
int l = temp.length;    
for(int a=0; a<l; a++){
SMTi[a] = Integer.parseInt(temp[a]); //NPE, when I just write the arguments above instead getters
    }

return SMTi;
    }
 }



public synchronized void addListener(UserListener listener)
{
    listeners.add(listener);
}

public synchronized void removeListener(UserListener listener)
{
    listeners.remove(listener);
}


protected synchronized void machineStart(int hour, int minute, int second)
{                  

            if(second == tStart[2] && 
                    minute == tStart[1] &&
                    hour == tStart[0]){


    System.out.println("START \n \n");        


    UserEvent ue = new UserEvent(this);

    for(UserListener ul: listeners)
    {
        ul.machineStart(ue);
    }
            }
}


    protected synchronized void machineStop(int hour, int minute, int second)
{               
           if(second == tStop[2] && 
                    minute == tStop[1] &&
                    hour == tStop[0]){
    System.out.println("STOP \n \n");
    UserEvent ue = new UserEvent(this);
    for(UserListener ul: listeners)
    {
        ul.machineStop(ue);
    }
            }
}


    protected synchronized void machinePause(int hour, int minute, int second)
{
            if(second == tPause[2] && 
                    minute == tPause[1] &&
                    hour == tPause[0]){
    System.out.println("PAUSE \n \n");
            Mode.pause = true;
    UserEvent ue = new UserEvent(this);
    for(UserListener ul: listeners)
    {
        ul.machinePause(ue);
    }
            }
}


    protected synchronized void machineResume(int hour, int minute, int second)
{
            if(second == tResume[2] && 
                    minute == tResume[1] &&
                    hour == tResume[0]){
    System.out.println("RESUME \n \n");
            Mode.pause = false;
    UserEvent ue = new UserEvent(this);
    for(UserListener ul: listeners)
    {
        ul.machineResume(ue);
    }
            }
}


    protected synchronized void machineRestart(int hour, int minute, int second)
{
        if(second == tRestart[2] && 
                    minute == tRestart[1] &&
                    hour == tRestart[0]){
    System.out.println("RESTART \n \n");
    UserEvent ue = new UserEvent(this);
    for(UserListener ul: listeners)
    {
        ul.machineRestart(ue);
    }
        }
}


}

我试图重新组织代码的各个部分,但没有帮助。我不知道发生了什么事。我需要有人在外面重新审视它。非常感谢。

你想要堆栈跟踪,这里是:)

run:
Exception in thread "main" java.lang.NullPointerException
at maszyna.User.splitMyTime(User.java:110)
at maszyna.User.<init>(User.java:93)
at maszyna.Main.main(Main.java:9)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
4

1 回答 1

2

根据您的 StackTrace,您可以执行以下步骤:

A)像这样更改属性的初始化:

int[] tStart = null; 
int[] tPause = null;
int[] tResume = null;
int[] tRestart = null;
int[] tStop = null;

B)添加一个方法来分割你的时间:

public void splitAllTimes() {
    this.tStart = splitMyTime(getTimeStart()); 
    this.tPause = splitMyTime(getTimePause()); 
    this.tResume = splitMyTime(getTimeResume());
    this.tRestart = splitMyTime(getTimeRestart());
    this.tStop = splitMyTime(getTimeStop());
}

C)将您的主要方法更改为:

public static void main(String[] args) {

    User user = new User(); 
    Mode.start = true;
    Mode.pause = false; 

    Machine machine = new Machine();
    user.addListener(machine);       


    user.setTimeStart("1:15:30");
    user.setTimePause("1:15:40");
    user.setTimeResume("1:15:50");
    user.setTimeRestart("1:16:00");
    user.setTimeStop("1:16:10");
    //------------------Add this line--------------------
    user.splitAllTimes();  //Add this line to init your attrbutes
}
于 2013-06-03T02:09:25.343 回答