HttpConnection throws NullPointerException

Hi everybody !

I'm faceing an odd problem here. I have developed an J2ME (MIDP1.0) application that, among other things, needs to comunicate with my server. The application woks fine on the emulator, it even works great on my phone (Nokia 2650) and on my partners phone (Nokia 6610), but when tested on other phones (SonyErricson, Sagem, Motorola, even an smartphone from Nokia, a 7610 i think) it throws a NullPointerException. I've run out of ideeas regading how to solve this. Here's the piece of code that bugs me:

publicvoid run(){

Pachet raspuns =null;

display.setCurrent(forma);

progress.setLabel("Trying to connect");

try{

conn = (HttpConnection) Connector.open(URL, Connector.READ_WRITE);

conn.setRequestMethod( HttpConnection.POST );

conn.setRequestProperty("Content-Type","application/vnd.wap.wmlc");

byte[] dataForSend = deTrimis.getRawData();

conn.setRequestProperty("Content-Length",""+dataForSend.length);

progress.setLabel("Sending request");

progress.setValue(25);

forma.append("Opening outputstream\n");

os = conn.openOutputStream();

forma.append("Sending data\n");

os.write( dataForSend );

forma.append("Flushing data\n");

os.flush();// this throws the exception

forma.append("Closing stream\n");

os.close();// if i remove the "os.flush()", the exception is thrown here

forma.append("Getting response code\n");

int code = conn.getResponseCode();// if i remove both os.flush() and os.close() the exception is thrown here !!!

forma.append("Response code = "+code+"\n");

progress.setLabel("Checking response");

progress.setValue(50);

if (code != HttpConnection.HTTP_OK){

thrownew Exception (conn.getResponseMessage()+"("+code+")");

}

progress.setLabel("Reading data");

progress.setValue(75);

if (!aborted){

dIn = conn.openDataInputStream();

raspuns =new Pachet();

raspuns.readPackage(dIn);

}

if (!aborted)

listener.packageReceived(raspuns);

}catch (Exception e){

forma.append("Connection failed. Reason: "+e.getMessage());

e.printStackTrace();

}finally{

cleanUp();

}

}

I really need to get this working, so could please someone give me a hint.

Thank you,

Daniel Comsa.

[3710 byte] By [dcomsaa] at [2007-10-2 4:55:44]
# 1

I found this because I seem to have the same problem. I am trying to do an HttpSconnection to a server. And my midlet runs on any phone a tested, Nokia, Siemens, of course it runs on the emulator as well.

But on a motorola L7 I get a null pointer exception when the connection changes the state from setup to connected. So any API call which initiates the state change gives this exception.

I could post my code, but this exception even happens with the sample code from j2me api reference

chapkina at 2007-7-16 1:00:05 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Hi all

I'm facing same problem on Motorola L7 , I checked the response code sent by the server and its "400" that means bad request and server could no handle , but when same URL is typed in "Go to URL" of Motorola L7 I get the response.

What must be the issue, is there anything that while sending data it encrypts or what ?

Please help me out

Regards,

Sushil

sushil_systema at 2007-7-16 1:00:05 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

Hi

I solved this problem.

There is nothing wrong with the device but the connection that we use.

Our service provider does not provide the pure HTTP connection rather it goes over WAP and thats where the problem comes.

Cause we attach our header for the server and WAP sticks his header in between , thats why server does not handle the request and consider it as bad request.

I have commented the following lines for my code to work

connection.setRequestProperty("User-Agent",

System.getProperty("microedition.profiles"));

connection.setRequestProperty("Content-Type",

"application/octet-stream");

Regards,

Sushil

sushil_systema at 2007-7-16 1:00:05 > top of Java-index,Java Mobility Forums,Java ME Technologies...