Chris James Hancocks
Chris James Hancocks

Reputation: 323

NPE when trying connect and send telnet commands

I'm hoping you can all help me out here.

Basically I have been having a few problems with my app. My previous questions on Stack have now all been solved so thanks to everyone who helped me out.

I am now getting a NPE but no idea why.

Basically my code is:

  1. Connecting to a device (IP address from an intent and fixed port of 32)
  2. Once connected send the command "root/r/n" twice. (This is the login for the device not the actual connection, the connection is not protected.
  3. Once there is return of "SNX_COM>" or "SCX_COM>" await commands.
  4. I then want to be able to send them commands from a button click.

The problem I have is that I cannot get the initial connection active. It would be grateful if someone could help me.

Java class:

package com.smarte.smartipcontrol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

public class IPControl extends Activity {

private Socket socket;
private static final int REDIRECTED_SERVERPORT = 32;
public PrintWriter out;
public BufferedReader in;
public String data;
public Object pd;
//get the message from intent
Intent intent = getIntent();
 String actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);

public void getModel(View view) {
    try {
        out.println("[m\r\n");
        //System.out.print("root\r\n");
        while(!in.ready());
        String textStatus = readBuffer();

    } catch(IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_ipcontrol);



    try {
        new AsyncAction().execute();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

private class AsyncAction extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... args) { 
        try {
            InetAddress serverAddr = InetAddress.getByName(actu_ip);
            socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
            OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
            BufferedWriter bw = new BufferedWriter(osw);
            out = new PrintWriter(bw, true); 
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (! in .ready());
            readBuffer();
            out.println("root\r\n");
            while (! in .ready());
            readBuffer();
            out.println("root\r\n");
            while (! in .ready());
            String msg = "";
            while ( in .ready()) {
                msg = msg + (char) in .read();
            }
            out.println("[c,l#,i5,o*\r\n");
            while (! in .ready());
            readBuffer();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;//returns what you want to pass to the onPostExecute()
    }

    protected void onPostExecute(String result) {

        //results the data returned from doInbackground

        IPControl.this.data = result;

    }
}

private String readBuffer() throws IOException {
    String msg = "";

    while(in.ready()) {
        msg = msg + (char)in.read();
    }
    //System.out.print(msg);
    if(msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
    else if(msg.indexOf("SCX_COM> ") != -1) return msg.substring(0, msg.indexOf("SCX_COM> "));
    else return msg;
}
}

LogCat:

12-07 09:29:24.596: E/AndroidRuntime(772): FATAL EXCEPTION: main
12-07 09:29:24.596: E/AndroidRuntime(772): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.smarte.smartipcontrol/com.smarte.smartipcontrol.IPControl}: java.lang.NullPointerException
12-07 09:29:24.596: E/AndroidRuntime(772):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-07 09:29:24.596: E/AndroidRuntime(772):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-07 09:29:24.596: E/AndroidRuntime(772):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-07 09:29:24.596: E/AndroidRuntime(772):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-07 09:29:24.596: E/AndroidRuntime(772):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 09:29:24.596: E/AndroidRuntime(772):  at android.os.Looper.loop(Looper.java:137)
12-07 09:29:24.596: E/AndroidRuntime(772):  at android.app.ActivityThread.main(ActivityThread.java:5039)
12-07 09:29:24.596: E/AndroidRuntime(772):  at java.lang.reflect.Method.invokeNative(Native Method)
12-07 09:29:24.596: E/AndroidRuntime(772):  at java.lang.reflect.Method.invoke(Method.java:511)
12-07 09:29:24.596: E/AndroidRuntime(772):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-07 09:29:24.596: E/AndroidRuntime(772):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-07 09:29:24.596: E/AndroidRuntime(772):  at dalvik.system.NativeStart.main(Native Method)
12-07 09:29:24.596: E/AndroidRuntime(772): Caused by: java.lang.NullPointerException
12-07 09:29:24.596: E/AndroidRuntime(772):  at com.smarte.smartipcontrol.IPControl.<init>(IPControl.java:29)
12-07 09:29:24.596: E/AndroidRuntime(772):  at java.lang.Class.newInstanceImpl(Native Method)
12-07 09:29:24.596: E/AndroidRuntime(772):  at java.lang.Class.newInstance(Class.java:1319)
12-07 09:29:24.596: E/AndroidRuntime(772):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-07 09:29:24.596: E/AndroidRuntime(772):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-07 09:29:24.596: E/AndroidRuntime(772):  ... 11 more

Thanks in advanced.

Upvotes: 0

Views: 270

Answers (1)

Luis
Luis

Reputation: 12048

The problem that prevents your app to start is in these two lines:

Intent intent = getIntent();
String actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);

I can't use getIntent() in the initialization, you should move it to the onCreate():

Intent intent;
String actu_ip;

@Override
public void onCreate(Bundle savedInstanceState) {
    ....
    Intent intent = getIntent();
    String actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
    ....
}

Regards.

Upvotes: 1

Related Questions