Click here to Skip to main content
12,239,973 members (60,012 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
Android. Java.Lang.NullPointerException execiting this code:
try{
//sc.setReceiveBufferSize(163840);
sc = new Socket("46.247.186.175", 8090);
SocketAddress sa = new InetSocketAddress("46.247.186.175", 8090);
sc.connect(sa);
   }catch(Exception x)
   {
       logn.setText(x.toString());//Java.Lang.NullPointerException, Why?

   }
EDIT: logn is declared in OnCreate func and I wrote "EditText logn;" in the Class
EDIT2: I have commented "sc.setReceiveBufferSize(163840);", so now app crashing without any exceptions.
Posted 12-Nov-12 8:14am
halabella1.3K
Edited 12-Nov-12 21:30pm
v6
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Apparently, because logn is null at this point. Look at your code and find out why. Should you have a smallest concern about your runtime behavior, always use the debugger.

—SA
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I second Sergey's answer, the Socket Connection is failing.

You should also write that different:

try{
  sc.setReceiveBufferSize(163840);
  sc = new Socket("46.247.186.175", 8090);
  SocketAddress sa = new InetSocketAddress("46.247.186.175", 8090);
  sc.connect(sa);
}
catch(Exception x) {
       System.out.println(x.printStackTrace()); // to get the full exception
   }

I also recommend better naming of variables - you will loose track with sc/sa/s... . Name them "socket" and "address" and so on.
  Permalink  
Comments
halabella 12-Nov-12 15:17pm
   
By adding "System.out.println(x.printStackTrace());" I got an error: "The method printIn(void) is undefined for the type PrintStream", what to do with this?
TorstenH. 12-Nov-12 15:21pm
   
maybe you should try desktop java development first.
halabella 12-Nov-12 15:38pm
   
I'm not understood you. I want to rewrite my totally worked JavaME app for Android. I have edited my post

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100