Greeting,

I'm currently programming a client-server connection with single socket...I use Linux C programming for server side, and J2me for client side, and here's the problem...
The login function run successfully for the server side, when It's a null exception for the client. The error is as follow:

java.lang.NullPointerException
at java.io.InputStream.read(InputStream.java:89)
at conn.Connectors.command(Connectors.java:62)
at mid.Midlet.commandAction(Midlet.java:136)
....

as for the code:

public class Connectors implements Runnable{
    public SocketConnection con;
    public InputStream is;
    public OutputStream os;
    public boolean exit = false, choosing = false;
    public Midlet mid;
    public String buff;
    public byte buffs[];
    public Connectors(Midlet mid){
        this.mid = mid;
    }
    public void run() {
        choosing = true;
        try
        {
            con = (SocketConnection) Connector.open("socket://"+mid.cf.getAddr()+":8999");
            os = con.openOutputStream();
            os.write("alow".getBytes());
            is = con.openInputStream();
        }
        catch (IOException ex) {
            choosing = false;
            exit = true;
            ex.printStackTrace();
        }
        choosing = false;
        while (!exit)
        {
        }
        try{
            os.write("exit".getBytes());
            is.close();
            os.close();
            con.close();
        }
        catch (Exception e)
        {}

    }
    public void stopNow()
    {
        exit = true;
    }
    public int command(String cmd,String args1,String args2)
    {
        try {
            if (cmd.equals("login"))
            {
                    os.write("login\n".getBytes());
                    Thread.sleep(10);
                    os.write((args1+ " " + args2).getBytes());
                    is.read(buffs);            // here is LINE NO 62
                    buff = buffs.toString();
                    System.out.println(buff);
                    if(buff.equals("acc")){
                        return 1;
                    }
            }
            else
            if (cmd.equals("reg"));
        }
        catch (Exception ex){
                ex.printStackTrace();
        }
        return -1;
    }

please help

Recommended Answers

All 6 Replies

> public byte buffs[];
How many bytes?
And does the function you're calling know this?

Don't you need to allocate some space here?

the size may vary depends on the string I send.

I might declare buffs as buffs = new byte[150];

but when I supposed to get "acc", I get something like [B@f828ed68 instead..

May be flush() is missing.

os.write(...)
os.flush()

not working :(

I tried to replace
buff = buffs.toString();
with
buff = new String(buffs, 0, buffs.length);

and it works :)
problem solved

thanks for replying

not working :(

adatapost>Where and when a command method is called?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.