0
所以我寫了一個程序,並有3個NPEs - 我不明白爲什麼,也許我是我的流感失明;)有人可以告訴我什麼是錯的?Java - 空指針異常,但我可以得到值
我不粘貼整個代碼,但只有重要的類。剩下的就好了。
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)
這將是有益的張貼堆棧跟蹤。 –
@LuiggiMendoza:我不確定。我無法用我嘗試過的模型重現NPE。 OP需要製作併發布[sscce](http://sscce.org)。 –
你有多少代碼?您當前的代碼不會重現NPE。 –