0

I am developing an application that contains libnodave library(Connecting with Siemens PLC). I want to simply read some data and put in on the android screen periodicly.The App is working on the emulator just fine, but on real device I am getting NullPointerException. I think my problem is kind of coming from usage of Java language but i am not sure about it. I am adding the code below:

Can anyone explain why this would be happening?

This is error message:

09-05 13:28:01.359: E/HAHAHA(16267): java.lang.NullPointerException
09-05 13:28:01.359: E/HAHAHA(16267):    at nodave.DataIsoTCP.ReadMyData(DataIsoTCP.java:83)
09-05 13:28:01.359: E/HAHAHA(16267):    at com.example.haus.EnglishScreen$2$1$1.run(EnglishScreen.java:95)
09-05 13:28:01.359: E/HAHAHA(16267):    at android.os.Handler.handleCallback(Handler.java:587)
09-05 13:28:01.359: E/HAHAHA(16267):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-05 13:28:01.359: E/HAHAHA(16267):    at android.os.Looper.loop(Looper.java:130)
09-05 13:28:01.359: E/HAHAHA(16267):    at android.app.ActivityThread.main(ActivityThread.java:3687)
09-05 13:28:01.359: E/HAHAHA(16267):    at java.lang.reflect.Method.invokeNative(Native Method)
09-05 13:28:01.359: E/HAHAHA(16267):    at java.lang.reflect.Method.invoke(Method.java:507)
09-05 13:28:01.359: E/HAHAHA(16267):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-05 13:28:01.359: E/HAHAHA(16267):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-05 13:28:01.359: E/HAHAHA(16267):    at dalvik.system.NativeStart.main(Native Method)

This is EnglishScreen.java

import nodave.DataIsoTCP;
import android.app.Activity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class EnglishScreen extends Activity {
Button read;
Button back;
Button next;
TextView user;
TextView bowlValue;
TextView scrollValue;
TextView productValue;
TextView polymerValue;
TextView flushValue;
TextView automaticValue;
//Intent i=new Intent();
String logiName="admin";
Handler myHandler = new Handler();
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.english);
    user=(TextView)findViewById(R.id.user);

    //textviews
    bowlValue=(TextView)findViewById(R.id.bowlValue);
    scrollValue=(TextView)findViewById(R.id.scrollValue);
    productValue=(TextView)findViewById(R.id.productValue);
    polymerValue=(TextView)findViewById(R.id.polymerValue);
    flushValue=(TextView)findViewById(R.id.flushValue);
    automaticValue=(TextView)findViewById(R.id.automaticValue);

    read=(Button)findViewById(R.id.read);
    back=(Button)findViewById(R.id.back);
    next=(Button)findViewById(R.id.Next);

    next.setVisibility(View.INVISIBLE);
    try{
        Intent i = getIntent();
        String login=i.getStringExtra("login");
        //i.putExtra("login", login);
        user.setText(login);
        if(logiName.equals(login)){
            next.setVisibility(View.VISIBLE);
        }

    }
        catch(Exception e){}

    try{
        DataIsoTCP.ConnectIsoTcp("192.168.1.10");}
        catch(Exception e){}

next.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent setIntent = new Intent(getApplicationContext(),SetScreen.class);
        startActivity(setIntent);
    }
});

    read.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    while (true) {
                        try {

                            myHandler.post(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        // TODO Auto-generated method stub
                                        // Write your code here to update the UI.
                                        //  bowlValue.setText(String.valueOf(DataIsoTCP.ReadMyData()));

                                        bowlValue.setText(String
                                                .valueOf(DataIsoTCP
                                                        .ReadMyData(3,
                                                                28,4)));
                                        //  scrollValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(4,30)));
                                        //  productValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(5,24)));
                                        //  polymerValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(6,10)));
                                        //  flushValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(7,24)));
                                        //  automaticValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(8,24)));
                                    } catch (Exception e) {
                                        // TODO: handle exception
                                        Log.e("HAHAHA","exception",e);
                                    }
                                }
                            });
                            Thread.sleep(5000);
                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                    }
                }
            }).start();
        }
    });
    //User name from main page


    //Back button function

    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });
    }
}

This is DataIsoTCP.java:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.R.bool;

public class DataIsoTCP {
public static boolean Connection = false;
public static int i, j;
public static long a, b, c;
public static float d, e, f;
public static char buf[];
public static byte buf1[];
public static PLCinterface di;
public static TCPConnection dc;
public static Socket sock;
public static int slot;
public static byte[] by;
public static String IP;

// IP 192.168.1.10
DataIsoTCP(String host) {
    IP = host;
    // Nodave.Debug=Nodave.DEBUG_ALL;
    buf = new char[Nodave.OrderCodeSize];
    buf1 = new byte[Nodave.PartnerListSize];
    try {
        sock = new Socket(host, 102);
    } catch (IOException e) {
        System.out.println(e);
    }
}

public static void StartConnection() {
    Connection = false;
    OutputStream oStream = null;
    InputStream iStream = null;
    slot = 2;

    if (sock != null) {
        try {
            oStream = sock.getOutputStream();
        } catch (IOException e) {
        }
        try {
            iStream = sock.getInputStream();
        } catch (IOException e) {
        }
        di = new PLCinterface(oStream, iStream, "IF1", 0,
                Nodave.PROTOCOL_ISOTCP);

        dc = new TCPConnection(di, 0, slot);
        int res = dc.connectPLC();
        if (0 == res) {
            Connection = true;
            System.out.println("Connection OK ");
        } else {
            System.out.println("No connection");
        }
    }
}

public static void StopConnection() {
    if (Connection == true) {
        Connection = false;
        dc.disconnectPLC();
        di.disconnectAdapter();
    }
}

// read 4 bytes from MD 100
public static long ReadData() {
    dc.readBytes(Nodave.FLAGS, 0, 100, 4, null);
    a = dc.getU32();
    return (long) a;
}
public static int ReadMyData(int db,int bol,int lenght){
    //dc = new TCPConnection(di, 0, slot);

    dc.readBytes(Nodave.DB, db, bol, lenght,null);
    a=dc.getU32();
    return (int)a;
} 

public static float ReadMyFloatData(int db,int bol){
        dc = new TCPConnection(di, 0, slot);
        dc.readBytes(Nodave.DB, db, bol, 4, null);
        d = dc.getFloat();

    return (float) d;
}

// write 4 bytes to MD 100
public static void WriteData(long a) {
    by = Nodave.bswap_32(a);
    dc.writeBytes(Nodave.FLAGS, 0, 100, 4, by);

}
public static void WriteMyData(long a){
    by=Nodave.bswap_32(a);
    dc.writeBytes(Nodave.DB, 4, 0, 100, by);
}
public static void WriteMyFloatData(float deger,int db,int bol){

        try {
            by = Nodave.toPLCfloat(deger);
            dc.writeBytes(Nodave.DB, db, bol, 4, by);
        } catch (Exception e) {
            // TODO: handle exception
        }

}
public static void WriteThat(int onOff){
    byte x=0;
    if(onOff==1){
         x=(byte) (x | (1<<1));
    }
    else
    {
        x=(byte)(x & ~(1<<1));
    }
    dc.writeBytes(Nodave.FLAGS, 0, 1, 1, by);
}

public static void ConnectIsoTcp(String adres) {

    Nodave.Debug = Nodave.DEBUG_ALL
            ^ (Nodave.DEBUG_IFACE | Nodave.DEBUG_SPECIALCHARS);

    DataIsoTCP tp = new DataIsoTCP(adres);
    tp.StartConnection();
}
}

In Manifest file i put

<uses-permission android:name="android.permission.INTERNET"/>
<activity android:name="com.example.haus.EnglishScreen"></activity>

TCPConnection.java :

package nodave;

public class TCPConnection extends S7Connection {

int rack;
int slot;

public TCPConnection(PLCinterface ifa, int rack, int slot) {
    super(ifa);
    this.rack = rack;
    this.slot = slot;
    PDUstartIn = 7;
    PDUstartOut = 7;
}

protected int readISOPacket() {
    int res = iface.read(msgIn, 0, 4);
    if (res == 4) {
        int len = 0x100 * msgIn[2] + msgIn[3];
        res += iface.read(msgIn, 4, len);
    } else {
        return 0;
    }
    return res;
}

protected int sendISOPacket(int size) {
    size += 4;
    msgOut[0] = (byte) 0x03;
    msgOut[1] = (byte) 0x0;
    msgOut[2] = (byte) (size / 0x100);
    msgOut[3] = (byte) (size % 0x100);
    if ((Nodave.Debug & Nodave.DEBUG_EXCHANGE) != 0) {
        Nodave.dump(" send packet", msgOut, 0, size);
    }
   // iface = new PLCinterface();  // i put it there
    iface.write(msgOut, 0, size);
    return 0;
}

public int exchange(PDU p1) {
    int res;
    PDU p2;
    if ((Nodave.Debug & Nodave.DEBUG_EXCHANGE) != 0) {
        System.out.println(" enter TCP.Exchange");
    }
    msgOut[4] = (byte) 0x02;
    msgOut[5] = (byte) 0xf0;
    msgOut[6] = (byte) 0x80;
    sendISOPacket(3 + p1.hlen + p1.plen + p1.dlen);
    res = readISOPacket();
    return 0;
}

/**
 * We have our own connectPLC(), but no disconnect() Open connection to a
 * PLC. This assumes that dc is initialized by daveNewConnection and is not
 * yet used. (or reused for the same PLC ?)
 */
public int connectPLC() {
    byte[] b4 = {
        (byte) 0x11,
        (byte) 0xE0,
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x01,
        (byte) 0x00,
        (byte) 0xC1,
        (byte) 0x02,
        (byte) 0x01,
        (byte) 0x00,
        (byte) 0xC2,
        (byte) 0x02,
        (byte) 0x01,
        (byte) 0x02,
        (byte) 0xC0,
        (byte) 0x01,
        (byte) 0x09};

    if ((Nodave.Debug & Nodave.DEBUG_CONNECT) != 0) {
        System.out.println("daveConnectPLC() step 1. rack:" + rack + " slot:" + slot);
    }
    System.arraycopy(b4, 0, msgOut, 4, b4.length);
    msgOut[17] = (byte) (rack + 1);
    msgOut[18] = (byte) slot;
    sendISOPacket(b4.length);
    readISOPacket();
    if ((Nodave.Debug & Nodave.DEBUG_CONNECT) != 0) {
        System.out.println("daveConnectPLC() step 1.");
    }
    return negPDUlengthRequest();
}
}
Mete
  • 31
  • 6
  • probably either the database is not being read, or we are failing at initialing tcp connection – Rachit Mishra Sep 05 '13 at 12:11
  • The NPE seems to be coming from this line of code: dc.readBytes(Nodave.DB, db, bol, lenght,null); Can you confirm you're successfully getting a TCP connection? – Prmths Sep 05 '13 at 15:39
  • i can successfully access the database. i was thinking the same way like you twntee, but why it is working on the simulator not on my phone ? i couldn't figured it out – Mete Sep 06 '13 at 14:46
  • Yes you are right @Prmths. Exception coming from that line.I tried to surround with if for checking dc is null or not. Apparently null.How i can i make that object not null?.I can add TCPconnection.java if you want to check. – Mete Sep 06 '13 at 14:56
  • What you will want to do is step through the StartConnection method and see what step in that process is failing. What I'm unclear on is that in your StartConnection method, dc is assigned and then immediately used to assign the res variable with an int value. This shows us that the dc variable its self is not null or your NPE would be there, not when you try to read the data. Can you post the code fro the TCPConnection class? – Prmths Sep 06 '13 at 16:02
  • Actually i took that DataIsoTCP and TCPConnection classes from nodave library .I didnt change anything on TCPConnection but i am adding the code anyways – Mete Sep 07 '13 at 06:19
  • I add the TCPConneciton java you can see in the end of the question – Mete Sep 07 '13 at 06:25
  • Thank you for your help, I figured it out. It is my own mistake i put the wrong ip address and its giving me a connection time out exception and after that Null pointer exception. What a mistake vuuh :) – Mete Sep 07 '13 at 08:23

0 Answers0