-1

I made a Java messaging app in Eclipse that runs fine as a JAR...now I am trying to make the same app using Android Studio in order to learn about basic android development...i don't understand why i get the error message "ChatAppAndroidClone keeps stopping".

in logCat it says:

2021-05-19 11:57:05.907 25336-25362/com.jasonkadams.chatappandroidclone E/AndroidRuntime: FATAL EXCEPTION: Thread-2 Process: com.jasonkadams.chatappandroidclone, PID: 25336 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.BufferedReader.readLine()' on a null object reference at com.jasonkadams.chatappandroidclone.MainActivity$IncomingReader.run(MainActivity.java:99) at java.lang.Thread.run(Thread.java:764)

Does anyone know why this might be happening? Thanks in advance

Here is my java code (just one class):

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    BufferedReader reader;
    PrintWriter writer;
    Socket sock;
    //String userName = System.getProperty("user.name");
    String emoji = "8===D";

    Button btn_send,btn_emoji;
    EditText outgoing;
    TextView incoming;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_send=findViewById(R.id.btn_send);
        btn_emoji=findViewById(R.id.btn_emoji);

        outgoing=findViewById(R.id.outgoing);
        incoming=findViewById(R.id.incoming);

        //set up networking and thread for incoming messages
        setUpNetworking();
        Thread readerThread = new Thread(new IncomingReader());
        readerThread.start();

        //BUTTONS//

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // if (outgoing.getText() != null)
                try {
                    writer.println("userName" + ": "+outgoing.getText().toString());
                    writer.flush();

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                outgoing.setText("");
                outgoing.requestFocus();
            }
        });

        btn_emoji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    writer.println("userName" + ": "+emoji);
                    writer.flush();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    //METHODS//
    private void setUpNetworking() {
        try {
            sock = new Socket("10.0.0.171", 5000);
            InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
            reader = new BufferedReader(streamReader);
            writer = new PrintWriter(sock.getOutputStream());
            System.out.println("Server connection established.");

            incoming.append("Server connection established." + "\n");

        } catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("Server connection failed.");

            incoming.append("Server connection failed." + "\n");
        }
    }

    // displays messages
    class IncomingReader implements Runnable {
        public void run() {
            String message;
            try {
                while ((message = reader.readLine()) != null) {

                    System.out.println("client read " + message);

                    incoming.append(message+"\n");
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
  • 1
    Please check your `LogCat` and paste the error. – Tonnie May 19 '21 at 16:52
  • sorry-here it is: 2021-05-19 11:57:05.907 25336-25362/com.jasonkadams.chatappandroidclone E/AndroidRuntime: FATAL EXCEPTION: Thread-2 Process: com.jasonkadams.chatappandroidclone, PID: 25336 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.BufferedReader.readLine()' on a null object reference at com.jasonkadams.chatappandroidclone.MainActivity$IncomingReader.run(MainActivity.java:99) at java.lang.Thread.run(Thread.java:764) – javaLearner May 19 '21 at 16:59
  • Please check line 99 on your activity, that is where the problem is; there is a NullPointerException – Tonnie May 19 '21 at 17:10

1 Answers1

0

Add <uses-permission android:name="android.permission.INTERNET"/> in your manifest (AndroidManifest.xml).

And of-course android won't allow you to do new Socket("10.0.0.171", 5000); in the main thread, that might be your next crash. For that, you can check other discussions in stackoverflow like Android - Connect to socket in separate thread

mrahimygk
  • 512
  • 5
  • 20