Exception in thread "main" java.lang.NullPointerException

i am having the problem below
Exception in thread “main” java.lang.NullPointerException
at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:70)
at org.HSim.net.HSIP.server.HSIPProtocolHandler.(HSIPProtocolHandler.java:39)
at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)
at testofhsip.Main.main(Main.java:30)
Java Result: 1
I am using MINA

  1. at testofhsip.Main.main
    /a simple app to test the API i am making on top of MINA
    public static void main(String[] args) throws IOException {
    HSIPrunner s = new HSIPrunner();
    s.Start();
    char[] r = “Hi”.toCharArray();
    s.write®;
    // TODO code application logic here
    }
  2. at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)
    HSIP = new HSIPProtocolHandler();

at org.HSim.net.HSIP.server.HSIPProtocolHandler.(HSIPProtocolHandler.java:39)
try {
char[] C = null;
checkInput(IS , C);
} catch (IOException ex) {
ex.printStackTrace();
}
4. at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:70)
char[] CB = C;
reader.read(CB , 0 , 20);
note:

  1. reader is:
    InputStreamReader reader;
    intilzed:
    rotected void processStreamIo(IoSession ioSession, InputStream inputStream, OutputStream outputStream) {
    try {
    reader = new InputStreamReader(inputStream , “ISO-8859-1”);
    OSW = new OutputStreamWriter(outputStream , “ISO-8859-1”);

    } catch(UnsupportedEncodingException e) {
        
    }
    

    }
    if someone could help i would be gratefull

You call checkInput with a char[] which is null.

Rather fishy isn’t it?

what do you mean checkinput intilizez the char by calling
reader.read(CB , 0 , 20):
which intilzese the char ;D i do not get why this exception is thrown

try little debuging…
put if x==null and then print out “is null” to check if it’s reall null when it reaches that line
NullExceptions are my favorite exceptions :slight_smile: they are really easy to fix :wink:

it still dose not work it dose not print CB null which is what i told it to if its null But the exception continues Exception in thread “main” java.lang.NullPointerException
at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:73)
at org.HSim.net.HSIP.server.HSIPProtocolHandler.(HSIPProtocolHandler.java:39)
at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)
at testofhsip.Main.main(Main.java:30)
Java Result: 1

PS i am using 1.6.0b81
also isn`t CB suposed to be null becose reader.read(CB , 0 ,20);
intlizes it
PS reader.read(CB , 0 , 20);
is InputStreamReader.read(CB , 0 , 20);

this is my new code it still dose not work
at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:73)
public void checkInput(String IS) throws IOException {
char[] CB = new char[20];
if(CB == null) {
System.out.println(“CB null”);
}
reader.read(CB , 0 , 20);
at org.HSim.net.HSIP.server.HSIPProtocolHandler.(HSIPProtocolHandler.java:39)
public HSIPProtocolHandler() {
super();
String IS = null;
try {

        checkInput(IS);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
    at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)

public void Start() throws IOException {

    HSIP = new HSIPProtocolHandler();
    SimpleServiceRegistry registry = new SimpleServiceRegistry();
    
    Service service = new Service("HSIP" , TransportType.SOCKET , PORT );
    registry.bind(service , HSIP);
    
}
    at testofhsip.Main.main(Main.java:30)

public static void main(String[] args) throws IOException {
HSIPrunner s = new HSIPrunner();
s.Start();
char[] r = “Hi”.toCharArray();
s.write®;
// TODO code application logic here
}
PS org.HSim.net.HSIP.server.HSIPrunner
is a thread

Could you post a proper stack trace with a clean code example?

The mix of stack trace and code all mashed together makes it very hard to read.

and please mark the line in code where nullexception is thrown

init:
deps-jar:
compile:
run:
Exception in thread “main” java.lang.NullPointerException
at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:73)
at org.HSim.net.HSIP.server.HSIPProtocolHandler.(HSIPProtocolHandler.java:39)
at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)
at testofhsip.Main.main(Main.java:30)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

package testofhsip;

import java.io.IOException;
import org.HSim.net.HSIP.server.HSIPrunner;

/**
 *
 * @author HandE
 */
public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        HSIPrunner s = new HSIPrunner();
        s.Start();
        char[] r = "Hi".toCharArray();        
        s.write(r);
        // TODO code application logic here
    }
    
}


package org.HSim.net.HSIP.server;

import java.io.IOException;
import java.util.logging.Logger;
import org.apache.mina.common.TransportType;
import org.apache.mina.registry.Service;
import org.apache.mina.registry.ServiceRegistry;
import org.apache.mina.registry.SimpleServiceRegistry;

/**
 *
 * @author HandE
 */
public class HSIPrunner extends Thread {
    private static final int PORT = 4444;
    private static Logger log =
       Logger.getLogger("java.net.HSIP");
    HSIPProtocolHandler HSIP;
    /** Starts the Server */
    public void Start() throws IOException {
        
        HSIP = new HSIPProtocolHandler();
        SimpleServiceRegistry registry = new SimpleServiceRegistry();
        
        Service service = new Service("HSIP" , TransportType.SOCKET , PORT );
        registry.bind(service , HSIP);
        
    }
 public void run() {
        //checks Input then sleeps for 100 mileseconds
        String IS = null;
        try {
            
            while(run = true) {
                HSIP.checkInput(IS);
                try {
                    if(HSIP.MaxUserNOReached = true) {
                        maxNO();
                        full = HSIP.MaxUserNOReached;
                    }
                    this.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }


public class HSIPProtocolHandler extends StreamIoHandler {
    
    private static Logger log =
       Logger.getLogger("hsipserver");
    private int userCount;
    /** Creates a new instance of HSIPProtocolHandler */
    public HSIPProtocolHandler() {
        super();
        String IS = null;
        try {
             
            checkInput(IS);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
InputStreamReader reader;
protected void processStreamIo(IoSession ioSession, InputStream inputStream, OutputStream outputStream) {
       try {
            reader = new InputStreamReader(inputStream , "ISO-8859-1");
            OSW = new OutputStreamWriter(outputStream , "ISO-8859-1");
            
       } catch(UnsupportedEncodingException e) {
           
       }
        
    }
public void checkInput(String IS) throws IOException {
       char[] CB = new char[20];
       if(CB == null) {
           System.out.println("CB null");
       }
      //line below throws Exception
      [u]reader.read(CB , 0 , 20);[/u] 
       char[] Local = CB;
       String pass = String.valueOf(CB , 20 , 7);
       String local = null;
       local = local.valueOf(Local , 0 , 1);
       
       if(local == "H") {
          userCount += 1;
          char[] name = null;
          String Name = local.valueOf(name , 1 , 10);
          newUser(userCount , Name , pass);     
          
       }
       
       IS.valueOf(CB);
    }

It appears “reader” is null.

Kev

yeah, when does processStreamIo get called?

Looks like its part of MINA, I’d suspect that empty catch{} block myself. What happens if that explcit encoding doesn’t work?

Kev

Sigh… empty catch block… not even a comment in it… tsk. It’s like you want the problems to be hidden.

Getting a bit disappointed with this recently to be honest, I mean is this a java gaming forum or a how to program forum?

Kev

You’ve noticed it too then…

I think it is ultimately a good sign for Java gaming… this place is getting more popular… more people are considering Java for games. But it does mean that there are going to be lots of newbies.

I’m not sure there is anything we can do to keep the conversations at the releatively high level that we have been acustomed to. Other than make sure the moderators keep the newbie posts in the appropriate forum.

Anyway, everyone has to learn somehow… I guess the veterans here have the responsibility of providing guidance.

??? ???

You are hiding your exception. When you do that you are just asking for trouble.


  protected void processStreamIo(IoSession ioSession, InputStream inputStream, OutputStream outputStream) {
    try {
      reader = new InputStreamReader(inputStream , "ISO-8859-1");
      OSW = new OutputStreamWriter(outputStream , "ISO-8859-1");
            
    } catch(UnsupportedEncodingException e) {
           
    }
  }

You should ALWAYS at the very least call

e.printStackTrace();

What is probably happening is this method is throwing an exception, which causes reader to not be initialized. You don’t know it because you swallow/hide the exception.

ok i am just going to scrap that code MINA is not that good but i`ve got a nother one in my code now >:( :frowning:
init:
deps-jar:
compile:
run:
startting…
got output stream
Exception in thread “main” java.lang.NullPointerException
at org.HSim.net.HSIP.server.ManServe.writeString(ManServe.java:131)
at testofhsip.Main.main(Main.java:30)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

 
public void writeString(String infotosend) throws NotStarted, IOException {
        if(started = true) {
            OSW.write(infotosend);
        } else {
            throw new NotStarted();
        }
        
    }

OSW is not null it is intalized in
org.HSim.net.HSIP.server.ManServe.run() before
write can be called at

 
public void run() { 
        try {
            h = serverSocket.accept();
            
            System.out.println("starting ....");
            log.finest("startting");
            h.setPerformancePreferences(2 , 0 , 1);
            h.setTcpNoDelay(true);
            h.setKeepAlive(true);
            h.setOOBInline(true);
            h.setSendBufferSize(2000);
            System.out.println("configuring.......");
            log.finest("configuring");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        OutputStream(); 

at


private void OutputStream() {
        try {
            OSW = new OutputStreamWriter(h.getOutputStream() , "ISO-8859-1");
            started = true;
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
            log.warning(ex.getMessage());
        } catch (IOException ex) {
            ex.printStackTrace();
            log.warning(ex.getMessage());
        }
    }

OSW.write(infotosend);

that’s line 131? Something in that line is null. It dosen’t have to be OSW, it can be infotosend. As before, just test it for null before just before that line.

OSW = new OutputStreamWriter(h.getOutputStream() , “ISO-8859-1”);
note h is a soket


public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        ManServe s = new ManServe();
        s.start();
        try {
            s.writeString("hi");
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (NotStarted ex) {
            ex.printStackTrace();
        }
        s.close();
        // TODO code application logic here
    }
    
}

s.writeString(“hi”);
is infotosend = “hi” :o :frowning: >:( :-[ :’( :’( :’( :’( :’( :’(