0

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

3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by __avd
0

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

Don't you need to allocate some space here?

0

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..

0

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

and it works :)
problem solved

thanks for replying

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.