2012-02-15 210 views
0

有空指针异常的问题,我读了几篇文章回合那个错误,仍然coundnt找出什么问题。java.lang.NullPointerException错误如何解决?

误差在CompatibleActivity[topIndex]=new Activity(aNum,bNum,c);

topIndex=0的方式发生。

任何人都可以突出显示我有问题吗?

这里我班

public class Schedulingtest { 


public static void main (String[] args) throws IOException 
{ 

    Scanner fileScan; 

    fileScan=new Scanner(new File("data.txt")); 

    Schedule compatibility = new Schedule(); 


    while(fileScan.hasNext()) 
    {String url=fileScan.nextLine(); 
    compatibility.addActivity(url); 


    } 

} 


public class Schedule{ 


import java.util.Scanner; 
import java.util.Arrays; 

public class Schedule { 
Activity[] CompatibleActivity; 
int totalTime=0,topIndex=0; 
Scanner urlScan; 

public Schedule(){ 
Activity[] CompatibleActivity=new Activity[30]; 

} 


public int getTotalTime() 
{return totalTime; 
}  

public void addActivity(String entry){ 

    urlScan=new Scanner(entry); 
    urlScan.useDelimiter(" "); 

    String c=null; 
    int aNum = 0,bNum=0; 

    while(urlScan.hasNext()) 
    {String a=urlScan.next(); 
    String b=urlScan.next(); 
    c=urlScan.next(); 

    aNum=Integer.parseInt(a); 
    bNum=Integer.parseInt(b); 

      }  
    CompatibleActivity[topIndex]=new Activity(aNum,bNum,c); 
    topIndex++; 

    System.out.println("Activity added: start "+aNum+ " stop "+bNum+" "+c); 
}  

}

活动课

public class Activity { 

private int start,stop,duration; 
private String name; 

public Activity(int Start,int Stop,String Name) 
{ 
start=Start; 
stop=Stop; 
name=Name; 
duration=Stop-Start; 
}  

public String getName() 
{return name; 
}   

public int getStart() 
{return start; 
} 

public int getStop() 
{return stop; 
}   

public int getDuration() 
{return duration; 
}  

public boolean compatible(int Start1,int Stop1,int toMatchsideStart,int toMatchsideStop) 
{ 
    int Start=Start1; 
    int Stop=Stop1; 
    int toMatchStart=toMatchsideStart; 
    int toMatchStop=toMatchsideStop; 

if(toMatchStop<=Start) 
{return true; 
} 
if(toMatchsideStart>=Stop) 
{return true; 
} 
else 
{return false;}  
}   


public String toString() 
{return(name+"<"+start+","+stop+">"); }   
} 
+1

什么是完整的堆栈跟踪? – DaveJohnston 2012-02-15 09:44:15

+1

你在哪里声明'CompatibleActivity',你在哪里初始化这个数组(以便它不为空)? – Thilo 2012-02-15 09:44:53

+0

请发布抛出的异常的完整堆栈跟踪。 – 2012-02-15 09:45:39

回答

1

最有可能你已经在你的类中声明CompatibleActivity的表达式。你需要给它分配使用足够的元素(例如,在你的构造函数)一个真正的数组:

CompatibleActivity = new Activity[myBigNumber]; 
+0

您的代码的另一个问题是,您过早终止'while'块,因此只有在扫描了最后一个元素后才会填充'CompatibleActivity',而不是针对每个扫描的活动。 – 2012-02-15 09:51:11

+0

其真正我在类的构造函数中添加compatibileActivity,我更改并添加行代码到最外面的调度全局类,它的工作原理。 我不明白为什么? – 2012-02-15 09:58:43

+0

您的代码问题之一是格式不正确。如果你相应地对所有的块进行格式化,你就会明白为什么这会起作用(除非你不熟悉Java语言),在这种情况下,我会推荐一个关于这个主题的好书,参见[this SO question](http:// stackoverflow .com/questions/167179/java-tutorial)供参考)。 – 2012-02-15 10:17:02

1

如果NullPointerException异常肯定是在该行的话,就只能通过CompatibleActivity被空所致。您需要在您的代码中找到声明该Array对象的位置,并确保它也已实例化,例如,

Activity[] CompatibleActivity = new Activity[size]; 
1

检查在访问其中一个单元格之前是否初始化了数组。你需要为

private Activity[] CompatibleActivity; 

它声明的引用,默认情况下它初始化为null

CompatibilityActivity = new Activity[1]; // or any other positve number if size is bigger 
+0

我做了30个空数组插槽的初始化,以便我读取的文件中的项目将被输入到插槽中。 – 2012-02-15 09:52:13

+0

然后,请仔细检查数组是否真的*初始化,当您使用它。只需在导致问题的行之前添加一个'System.out.println(CompatibilityActivity.length)'即可。 (我猜,NPE会移动到*那个*新行) – 2012-02-15 09:55:21

0
Activity[] CompatibleActivity = new Activity[size]; 
// put some element in it like 
CompatibleActivity[0]=new CompatibleActivity();