NullPointerException problem T_T

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 6
Reputation: keicola is an unknown quantity at this point 
Solved Threads: 0
keicola keicola is offline Offline
Newbie Poster

NullPointerException problem T_T

 
0
  #1
Sep 30th, 2009
I'm trying to read a file which contains:
Dante
Beatrice
3
5
and put them into an array. Then I will assign the values of each member of the array to static variables. when i run the main class of my project, it throws a NullPointerException starting from the line in bold T_T i dunno what to do T_T

can anyone please please help me? i badly need this for my project T_T


 public static void loadPositions(){
          try{
             BufferedReader loadPlayerPositions = new BufferedReader(new FileReader("/media/KEI-SAMA/CS11/JOKE CS11/KnowGoMilestone5/playerpositions.txt"));

             String info[] = new String[3];
             
             for (int l = 0; l < info.length; l++){
             info[l] = loadPlayerPositions.readLine();

             info[0] = Players.nameOfPlayer1.toString();
             info[1] = Players.nameOfPlayer2.toString();
             info[2] = Players.loadplayer1currentblock;
             info[3] = Players.loadplayer2currentblock;
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: NullPointerException problem T_T

 
0
  #2
Sep 30th, 2009
For reading lined from a file:
          try{
             BufferedReader loadPlayerPositions = new BufferedReader(new FileReader("/media/KEI-SAMA/CS11/JOKE CS11/KnowGoMilestone5/playerpositions.txt"));

String line = loadPlayerPositions.readLine();
while (line!=null) {
  System.out.println("Line read: "+line);

   line = loadPlayerPositions.readLine();
}
} catch (Exception e) {
  System.out.println("Exception: "+e.getMessage());
}

With that way you read each line.
Do some checking in case the file doesn't have all 4 lines that you want.
You get the exception at that line:
info[0] = Players.nameOfPlayer1.toString(); probably because Players.nameOfPlayer1 is null
Last edited by javaAddict; Sep 30th, 2009 at 7:17 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: ejosiah is an unknown quantity at this point 
Solved Threads: 12
ejosiah's Avatar
ejosiah ejosiah is offline Offline
Junior Poster

Re: NullPointerException problem T_T

 
0
  #3
Sep 30th, 2009
In addition to your current problem you are bordering on ArrayIndexOutOfBounds Exception with this line of code
  1. info[3] = Players.loadplayer2currentblock;
. your array was initialised with a size of 3 and you are quering fo the 4th item in the array.

Either you change your array size to 4 or do not request for the 4th item
Knowledge is power but love conquers all
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: keicola is an unknown quantity at this point 
Solved Threads: 0
keicola keicola is offline Offline
Newbie Poster

Re: NullPointerException problem T_T

 
0
  #4
Sep 30th, 2009
javaAddict and ejosiah, thanks for your help!! :]
i've initialized the variables,
i've increased the array size to 4,
now it throws the IOException T_T
Last edited by keicola; Sep 30th, 2009 at 10:08 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: NullPointerException problem T_T

 
0
  #5
Sep 30th, 2009
Put a e.printstacktrace at the catch block, post your new code with the errors that you get
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: keicola is an unknown quantity at this point 
Solved Threads: 0
keicola keicola is offline Offline
Newbie Poster

Re: NullPointerException problem T_T

 
0
  #6
Oct 1st, 2009
I tried putting e.printStackTrace() but it didn't accept it saying that "void" cannot be used. When i checked, printStackTrace() was "void" after all. I tried using getMessage() and I got a message saying "Stream closed" when I tried running the program again.

  1. catch (IOException e){
  2. JOptionPane.showMessageDialog(null, "IOException: " +
  3. e.getMessage());
  4. }
Attached Thumbnails
Screenshot.png  
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: NullPointerException problem T_T

 
0
  #7
Oct 1st, 2009
Originally Posted by keicola View Post
I tried putting e.printStackTrace() but it didn't accept it saying that "void" cannot be used. When i checked, printStackTrace() was "void" after all. I tried using getMessage() and I got a message saying "Stream closed" when I tried running the program again.

  1. catch (IOException e){
  2. JOptionPane.showMessageDialog(null, "IOException: " +
  3. e.getMessage());
  4. }
  1. catch (IOException e){
  2. System.out.println(e.getMessage()); //returns String
  3.  
  4. JOptionPane.showMessageDialog(null, "IOException: " +
  5. e.getMessage());
  6.  
  7. e.printStackTrace(); // is void
  8. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: keicola is an unknown quantity at this point 
Solved Threads: 0
keicola keicola is offline Offline
Newbie Poster

Re: NullPointerException problem T_T

 
0
  #8
Oct 1st, 2009
I added e.printStackTrace just as you have instructed. After running the program, the IOException points to the line in bold. This were the lines that appeared at the bottom tab:

java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(BufferedReader.java:97)
at java.io.BufferedReader.readLine(BufferedReader.java:292)
at java.io.BufferedReader.readLine(BufferedReader.java:362)

at knowgomilestone4.StateOfGame.loadPositions
(StateOfGame.java:67)
at knowgomilestone4.StoryLine.mainMenu(StoryLine.java:64)
at knowgomilestone4.PlayEngine.main(PlayEngine.java:37)

StateOfGame.loadPositions is the method below
Storyline.mainMenu and PlayEngine.main are methods that call on StateOfGame.loadPositions so I think the problem may only be in the StateOfGame.loadPositions method.

       public static void loadPositions(){
          try{
             BufferedReader loadPlayerPositions = new BufferedReader(new 
             FileReader("/media/KEI-SAMA/CS11/JOKE CS11/KnowGoMilestone5
             /playerpositions.txt"));
             
             String info[] = new String[4];
             
             for (int l = 0; l < info.length; l++){
             info[l] = loadPlayerPositions.readLine();

             info[0] = Players.nameOfPlayer1.toString();
             info[1] = Players.nameOfPlayer2.toString();
             info[2] = Players.loadplayer1currentblock;
             info[3] = Players.loadplayer2currentblock;

             Players.loadCurrentPositions();
             Players.currentPositions();

             loadPlayerPositions.close();

             }

          } catch (FileNotFoundException e){
               JOptionPane.showMessageDialog(null, "File Not Found! :O");
          } catch (IOException e){
               JOptionPane.showMessageDialog(null, "IOException: " + e.getMessage());
               e.printStackTrace();
          }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: NullPointerException problem T_T

 
1
  #9
Oct 2nd, 2009
for (int l = 0; l < info.length; l++){
             info[l] = loadPlayerPositions.readLine();

             info[0] = Players.nameOfPlayer1.toString();
             info[1] = Players.nameOfPlayer2.toString();
             info[2] = Players.loadplayer1currentblock;
             info[3] = Players.loadplayer2currentblock;

             Players.loadCurrentPositions();
             Players.currentPositions();

             loadPlayerPositions.close();

       }

You are inside the loop, you close the BufferedReader and when the loop runs again you try to read from a closed BufferedReader. When you close the BufferedReader you cannot read from it again. Close it after you are done reading.
Also I believe that you should check if the line read:
info[l] = loadPlayerPositions.readLine(); is not null in case the file has less than 4 line
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: keicola is an unknown quantity at this point 
Solved Threads: 0
keicola keicola is offline Offline
Newbie Poster

Re: NullPointerException problem T_T

 
0
  #10
Oct 3rd, 2009
Gaaah. No wonder xd
thank you thank you so much!! :]
Reply With Quote Quick reply to this message  
Reply

Tags
nullpointerexception

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 323 | Replies: 9
Thread Tools Search this Thread



Tag cloud for nullpointerexception
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC