4 Replies - 1662 Views - Last Post: 21 May 2014 - 03:06 AM

#1 gentlepaul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-May 14

java.lang.NullPointerException

Posted 19 May 2014 - 05:35 AM

Hello all, please any help I can get will be appreciated I'm trying to save a game I'm working on in j2me, when I implement it the first time it works but the subsequent time it gives me an error and am using netbeans 6.9
<here is the error message> java.lang.NullPointerException: 0
at luCodes.GameInfo.WriteRecord(GameInfo.java:83)
at luCodes.LuCanvas.writeToGameInfo(LuCanvas.java:551)
at luCodes.LuMidlet.exitGame(LuMidlet.java:68)
at luCodes.LuMenu.commandAction(LuMenu.java:63)
at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46
at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74
at com.sun.midp.chameleon.layers.SoftButtonLayer.soft1(), bci=37
at com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(), bci=36
at com.sun.midp.chameleon.Cwindow.keyInput(), bci=38
at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17
at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
at com.sun.midp.events.EventQueue.run(), bci=179
at java.lang.Thread.run(Thread.java:724)......
public void WriteRecord(){
    openConnection();
  //  if (gameCanvas.gameOn == true )
   try{
   postings =  new int[] {gameCanvas.r1.getX(),gameCanvas.r1.getY(),gameCanvas.r2.getX(),gameCanvas.r2.getY(),
  gameCanvas.r3.getX(),gameCanvas.r3.getY(),gameCanvas.r4.getX(),gameCanvas.r4.getY(),gameCanvas.y1.getX(), gameCanvas.y1.getY(),
  gameCanvas.y2.getX(),gameCanvas.y2.getY(),gameCanvas.y3.getX(),gameCanvas.y3.getY(),gameCanvas.y4.getX(),gameCanvas.y4.getY(),
  gameCanvas.b1.getX(),gameCanvas.b1.getY(),gameCanvas.b2.getX(),gameCanvas.b2.getY(),gameCanvas.b3.getX(),gameCanvas.b3.getY(),
  gameCanvas.b4.getX(),gameCanvas.b4.getY(),gameCanvas.g1.getX(),gameCanvas.g1.getY(),gameCanvas.g2.getX(),gameCanvas.g2.getY(),
 gameCanvas.g3.getX(),gameCanvas.g3.getY(),gameCanvas.g4.getX(),gameCanvas.g4.getY(),gameCanvas.r1.steps,gameCanvas.r2.steps,
gameCanvas.r3.steps,gameCanvas.r4.steps,gameCanvas.y1.steps,gameCanvas.y2.steps,gameCanvas.y3.steps,gameCanvas.y4.steps,
gameCanvas.b1.steps,gameCanvas.b2.steps,gameCanvas.b3.steps,gameCanvas.b4.steps,gameCanvas.g1.steps,gameCanvas.g2.steps,
        gameCanvas.g3.steps,gameCanvas.g4.steps};
    for(int i = 0; i < postings.length; i++){
        
        //    System.out.println(postings[i]);
        // create a byte stream to write to
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(postings[i]);
        dos.flush();
        // add the record
        byte[] recordOut = baos.toByteArray();
        int newRecordId =
        rs.addRecord(recordOut, 0, recordOut.length);
        System.out.println("Adding new record: " + newRecordId + " Value: " + recordOut.toString());
        baos.close();
        dos.close();..................

    public void exitGame(){
            if(gameCanvas.gameOn == true){
        gameCanvas.writeToGameInfo();
        persistenceModel();
           }
     destroyApp(false); 
   ...............

  public void writeToGameInfo(){
      gInfo.WriteRecord();
  }...................


the problem is that the game saves the first time I exit, but when I start the game and it continues from where I saved it when I exit again it generates the error. I will be very grateful for your help

This post has been edited by jon.kiparsky: 19 May 2014 - 06:01 AM
Reason for edit:: [code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: java.lang.NullPointerException

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: java.lang.NullPointerException

Posted 19 May 2014 - 06:07 AM

Something is null at line 83. Unfortunately your post doesn't correspond with the actual line numbers in your source
Was This Post Helpful? 0
  • +
  • -

#3 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 134
  • Joined: 29-March 09

Re: java.lang.NullPointerException

Posted 19 May 2014 - 10:16 AM

As g00se stated, it's in line 83 of your code. How do we know this? Take a look at the stack trace:
java.lang.NullPointerException: 0
at luCodes.GameInfo.WriteRecord(GameInfo.java:83)



It's a NullPointerException, so we know why the error occurred...but where? The next line down tells you the Method it occurred in(WriteRecord), then the class followed by the line number (GameInfo.java and 83)
Was This Post Helpful? 0
  • +
  • -

#4 gentlepaul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-May 14

Re: java.lang.NullPointerException

Posted 20 May 2014 - 09:29 PM

I'm sorry I don't mean to bore you with too many lines of code I know where the error is but why its giving me that error is what I don't understand this is the line giving the error ...postings = new int[] {gameCanvas.r1.getX(),gameCanvas.r1.getY(),gameCanvas.r2.getX(),gameCanvas.r2.getY(), i.e. after the try statement. I have also included the midlet code. When I exit the game the first time, it works well and the game is saved but when I resume the game and continue and then try to save again by exiting it generates that error, why is it not giving me that error the first time? The writeRecord method is called when I exit the game. I want to be able to exit the game and resume the game more than once during the same game. I'll be grateful for help

public class LuMidlet extends MIDlet{
 private LuMenu menu;
    private LudoSplash splash;
    private Display display;
    private LuCanvas gameCanvas;
    private GameInfo info;
    public boolean value = false;
    private boolean gameInProgress = false;
    public LuMidlet() throws Exception {
        menu = new LuMenu(this);
        splash = new LudoSplash(this);
       info = new GameInfo(gameCanvas);
        gameInProgress = true;
    }
    public void startApp() {
       // setStore();
        if(display == null)
            display = Display.getDisplay(this);
        display.setCurrent(splash);
    }
    public void pauseApp() {
    }
    public void destroyApp(boolean unconditional) {
        System.gc();
        notifyDestroyed();
    }
    public void showMenu(){
         resumeGameMenu();
        display.setCurrent(menu);
    }
    public void startGame(){
        gameCanvas = null;
        try{
            gameCanvas = new LuCanvas(this);
            gameCanvas.start();
            display.setCurrent(gameCanvas);
            }catch(Exception e){e.printStackTrace();}
          gameCanvas.CloseGameInfo();
    }
    public void exitGame(){
         if(gameCanvas.gameOn == true){
        gameCanvas.writeToGameInfo();
            }
     destroyApp(false);
       }
       private void resumeGameMenu(){
   if(ReadStore() == true){
    menu.checkSaved(true);
    Display.getDisplay(this).setCurrent(menu);
   }
   }
  void resumeGame(){
  gameCanvas = null;
        try{
            gameCanvas = new LuCanvas(this,info);
            gameCanvas.start();
            display.setCurrent(gameCanvas);
            gameInProgress = true;
        }catch(Exception e){e.printStackTrace();}
  deleteRecords();
  gameCanvas.CloseGameInfo();
  gameCanvas.deleteStores();
  }
}



public class GameInfo {
 private LuCanvas gameCanvas;
    private int[] postings = new int[48];
      private LuMenu menu;
private RecordStore rs;
public GameInfo(LuCanvas gameCanvas){
    super();
    this.gameCanvas = gameCanvas;
}
private boolean openConnection(){
    if(rs == null){
        try{
            rs = RecordStore.openRecordStore("GameInfo", true);
            return true;
        }catch(RecordStoreException e){
           e.printStackTrace();
           return false;
        }
    }
    return false;
}
public void WriteRecord(){
    openConnection();  
        try{
   postings =  new int[] {gameCanvas.r1.getX(),gameCanvas.r1.getY(),gameCanvas.r2.getX(),gameCanvas.r2.getY(),
  gameCanvas.r3.getX(),gameCanvas.r3.getY(),gameCanvas.r4.getX(),gameCanvas.r4.getY(),gameCanvas.y1.getX(), gameCanvas.y1.getY(),
  gameCanvas.y2.getX(),gameCanvas.y2.getY(),gameCanvas.y3.getX(),gameCanvas.y3.getY(),gameCanvas.y4.getX(),gameCanvas.y4.getY(),
  gameCanvas.b1.getX(),gameCanvas.b1.getY(),gameCanvas.b2.getX(),gameCanvas.b2.getY(),gameCanvas.b3.getX(),gameCanvas.b3.getY(),
  gameCanvas.b4.getX(),gameCanvas.b4.getY(),gameCanvas.g1.getX(),gameCanvas.g1.getY(),gameCanvas.g2.getX(),gameCanvas.g2.getY(),
 gameCanvas.g3.getX(),gameCanvas.g3.getY(),gameCanvas.g4.getX(),gameCanvas.g4.getY(),gameCanvas.r1.steps,gameCanvas.r2.steps,
gameCanvas.r3.steps,gameCanvas.r4.steps,gameCanvas.y1.steps,gameCanvas.y2.steps,gameCanvas.y3.steps,gameCanvas.y4.steps,
gameCanvas.b1.steps,gameCanvas.b2.steps,gameCanvas.b3.steps,gameCanvas.b4.steps,gameCanvas.g1.steps,gameCanvas.g2.steps,
        gameCanvas.g3.steps,gameCanvas.g4.steps};
    for(int i = 0; i < postings.length; i++){
                
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(postings[i]);
        dos.flush();
        // add the record
        byte[] recordOut = baos.toByteArray();
        int newRecordId =
        rs.addRecord(recordOut, 0, recordOut.length);
        System.out.println("Adding new record: " + newRecordId + " Value: " + recordOut.toString());
        baos.close();
        dos.close();
     
    }
   postings = new int[48];
    }catch(Exception ioe){ioe.printStackTrace();}
    
}
public int ReadRecord(int num){
    openConnection();
    int value = 0;
    try{
        int recordSize = rs.getRecordSize(num);
        if(recordSize > 0){
     
 ByteArrayInputStream bais = new ByteArrayInputStream(rs.getRecord(num));
    DataInputStream dis = new DataInputStream(bais);
      value = dis.readInt();

System.out.println("Retrieved record: " + num + " Value: " + value);
    dis.close();
    bais.close();

 }
     }catch(Exception rse){
    rse.printStackTrace();
}
 
return value;
}
public void closeRecord(){
    if(rs != null){ System.out.println("info record is not null");
    try{
   // rs.deleteRecord(1);
    rs.closeRecordStore();    
}catch(RecordStoreException rse){rse.printStackTrace();}
}
    }
public void deleteStores(){
    try{System.out.println("Attempting to delete info");
        RecordStore.deleteRecordStore("GameInfo");
    }catch(RecordStoreException rse){System.out.println("Error deleting gameInfo"); rse.printStackTrace();}
}
}


This post has been edited by macosxnerd101: 20 May 2014 - 09:44 PM
Reason for edit:: Added code tags. Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: java.lang.NullPointerException

Posted 21 May 2014 - 03:06 AM

The first thing you need to do is find WHAT is null. That's easy enough to do with appropriate sysouts before the line that's giving the NPE
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1