0

I am a newbie in android studio.I am creating a app which will pass the data between PC and mobile using socket. I have combined the command based program with android. In this case the client send the number to server and server will check is it prime or not, according to it, server will send the reply to client.But i am getting fatal exception while launching the main activity.

Logcat is:

08-18 11:59:26.977 1728-1728/com.example.admin.test_purpose W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb4e68288)
08-18 11:59:26.977 1728-1728/com.example.admin.test_purpose E/AndroidRuntime: 
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.admin.test_purpose/com.example.admin.test_purpose.MainActivity}: java.lang.NullPointerException

 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
 at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1825)
at com.example.admin.test_purpose.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

Here is my source files

command based server program

import java.net.*; 
import java.io.*;

class tcpServerPrime {
public static void main(String args[]) {
    try {

        ServerSocket ss = new ServerSocket(8001); 
        System.out.println("Server Started..............."); 
        Socket s = ss.accept();
        DataInputStream in = new DataInputStream(s.getInputStream()); 
        int x= in.readInt();
        DataOutputStream otc = new DataOutputStream(s.getOutputStream()); 
        int y = x/2;

          if(x ==1 || x ==2 || x ==3) {
            otc.writeUTF(x + "is Prime"); 
            System.exit(0);
            }
          for(int i=2; i<=y; i++) {
            if(x%i != 0) {
            otc.writeUTF(x + " is Prime");
            } else {
            otc.writeUTF(x + " is not Prime");
            }

          }
    }catch(Exception e) {
        System.out.println(e.toString());
      }
 }

}

Main activity

package com.example.admin.test_purpose;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.net.*;
import java.io.*;


public class MainActivity extends Activity {
    EditText user_num = (EditText)findViewById(R.id.editText);
    TextView reply  = (TextView)findViewById(R.id.textView);
    Button btnsbm = (Button)findViewById(R.id.button);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onclicklistener();
    }
    public void onclicklistener(){
        btnsbm.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                             try {
                                Socket cs = new Socket("192.168.1.101", 8001);
                                int a = Integer.parseInt(user_num.getText().toString());
                                DataOutputStream out = new DataOutputStream(cs.getOutputStream());
                                out.writeInt(a);
                                DataInputStream in = new DataInputStream(cs.getInputStream());
                                reply.setText(in.readUTF());
                                cs.close();
                            } catch (Exception e) {
                                Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                            }
                    }
                }
        );
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_gravity="center_horizontal"
        android:hint="@string/user_value"
        android:layout_marginTop="15dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/submit"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.test_purpose">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Nilesh Indalkar
  • 310
  • 4
  • 16

2 Answers2

2

The problem is with those three lines:

EditText user_num = (EditText)findViewById(R.id.editText);
TextView reply  = (TextView)findViewById(R.id.textView);
Button btnsbm = (Button)findViewById(R.id.button);

The problem is, you're invoking findViewById() before the view is even initialized.

Move the findViewById-components to onCreate(), then it should work:

EditText user_num;
TextView reply;
Button btnsbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    user_num = (EditText)findViewById(R.id.editText);
    reply  = (TextView)findViewById(R.id.textView);
    btnsbm = (Button)findViewById(R.id.button);
    onclicklistener();
}
UeliDeSchwert
  • 1,146
  • 10
  • 23
0

Update you mainactivity

EditText user_num;
TextView reply;
Button btnsbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    user_num = (EditText)findViewById(R.id.editText);
    reply  = (TextView)findViewById(R.id.textView);
    btnsbm = (Button)findViewById(R.id.button);
    onclicklistener();
}
Rahul
  • 1,380
  • 1
  • 10
  • 24