0

having problem with null pointer exception and i read few article bout that error and still coundnt figure out what the problem.

The error happen at CompatibleActivity[topIndex]=new Activity(aNum,bNum,c);

topIndex=0 by the way.

Can anyone highlight the problem im having?

here my class

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

}

Activity Class

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+">"); }        
}
V_Stack
  • 147
  • 2
  • 7
  • 15
  • 1
    where do you declare `CompatibleActivity` and where do you initialize this array (so that it is not null)? – Thilo Feb 15 '12 at 09:44
  • Please post the complete stacktrace of the exception thrown. – Buhake Sindi Feb 15 '12 at 09:45
  • you are showing code that is of no interest and not showing the only piece of code that is important: declaration and initialization of `CompatibleActivity`. chances are that you didn't initialize it correctly. – vulkanino Feb 15 '12 at 09:47

4 Answers4

1

If the NullPointerException is definitely on that line, then it can only be caused by CompatibleActivity being null. You will need to find in your code where that Array Object is declared and make sure that it is also instantiated, e.g.

Activity[] CompatibleActivity = new Activity[size];
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
1

Check if you've initialized the array before you access one of its cells. You need an expression like

CompatibilityActivity = new Activity[1];  // or any other positve number if size is bigger
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • i did initialize with 30 Slot of empty array so that item in file i read will be entered to the slot. – V_Stack Feb 15 '12 at 09:52
  • Then please double check if the array is *really* initialized when you use it. Just add a `System.out.println(CompatibilityActivity.length)` right before the line that causes trouble. (I guess, the NPE then moves to *that* new line) – Andreas Dolk Feb 15 '12 at 09:55
1

Most likely you have CompatibleActivity declared in your class as

private Activity[] CompatibleActivity;

which declares a reference and initializes it to null by default. You need to assign it a real array with enough elements (e.g. in your constructor):

CompatibleActivity = new Activity[myBigNumber];
Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
  • Another issue with your code is that you terminated the `while` block too early, so filling in `CompatibleActivity` takes place only after the last element has been scanned, not for each of the scanned activities. – Alexander Pavlov Feb 15 '12 at 09:51
  • its true i added compatibileActivity in the constructor of the class and i change and added the line code to the outer most Schedule global class and it works. i dont understand why? – V_Stack Feb 15 '12 at 09:58
  • One of your problems with the code is poor formatting. If you format all your blocks accordingly, you will see why this works (unless you are not familiar with the Java language. In that case I would recommend a good reading on the topic. See [this SO question](http://stackoverflow.com/questions/167179/java-tutorial) for the reference). – Alexander Pavlov Feb 15 '12 at 10:17
  • ok. i will read that. Thx alot. – V_Stack Feb 15 '12 at 10:19
0
Activity[] CompatibleActivity = new Activity[size];
// put some element in it like 
CompatibleActivity[0]=new CompatibleActivity();
Tunaki
  • 132,869
  • 46
  • 340
  • 423