OK at first I am a totally noob in programming for Android (I know how to program calculator e.t.c.) so I tried to port a program from my finished .jar to a runnable Android program.

But when I try to start my Program, there is an Error on my Device: Unforunately, TvB-Chat has stopped.So this is my Servercode:

import java.awt.BorderLayout;import java.awt.Dimension;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.StringReader;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.Iterator;import javax.swing.JEditorPane;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.ScrollPaneConstants;import javax.swing.text.BadLocationException;import javax.swing.text.html.HTMLEditorKit;public class Server {ServerSocket server;ArrayList<PrintWriter> list_clientWriter;final int LEVEL_ERROR=1;final int LEVEL_NORMAL=0;public static void main(String[] args) {Server s=new Server();if (s.runServer()) {s.listenToClients();} else {// Do nothing}}public class ClientHandler implements Runnable {Socket client;BufferedReader reader;public ClientHandler(Socket client) {try {this.client=client;reader=new BufferedReader(new InputStreamReader(client.getInputStream()));} catch (IOException e) {e.printStackTrace();}}@Overridepublic void run() {String nachricht;try {while((nachricht=reader.readLine()) !=null) {appendTextToConsole("Vom Client: \n" + nachricht, LEVEL_NORMAL);sendToAllClients(nachricht);}} catch (IOException e) {e.printStackTrace();}}}public void listenToClients() {while(true) {try {Socket client=server.accept();PrintWriter writer=new PrintWriter(client.getOutputStream());list_clientWriter.add(writer);Thread clientThread=new Thread(new ClientHandler(client));clientThread.start();} catch (IOException e) {e.printStackTrace();} }}public boolean runServer() {try {server=new ServerSocket(5555);//PortappendTextToConsole("Server wurde gestartet!", LEVEL_ERROR);list_clientWriter=new ArrayList<PrintWriter>();return true;} catch (IOException e) {appendTextToConsole("Server konnte nicht gestartet werden!", LEVEL_ERROR);e.printStackTrace();return false;}}public void appendTextToConsole(String message, int level) {if(level==LEVEL_ERROR) {System.err.println(message + "\n");} else {System.out.println(message + "\n");}}public void sendToAllClients(String message) {Iterator it=list_clientWriter.iterator();while(it.hasNext()) {PrintWriter writer=(PrintWriter) it.next();writer.println(message);writer.flush();}}}

And this is my Client:

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.method.ScrollingMovementMethod;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;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 {EditText ETNameInput;EditText ETTextInput;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ETNameInput=(EditText) findViewById(R.id.NameInput);ETTextInput=(EditText) findViewById(R.id.TextInput);Button Go=(Button) findViewById(R.id.GOBUTTON);Thread t=new Thread(new MessagesFromServerListener());Go.setOnClickListener (new View.OnClickListener() {@Overridepublic void onClick(View v) {if (connectToServer()) {String JNI=ETNameInput.getText().toString();String JTI=ETTextInput.getText().toString();writer.println(JNI + ": " + JTI);writer.flush();} else {Toast.makeText(getApplicationContext(), "Internet ist nicht verfügbar.", Toast.LENGTH_SHORT).show();}}});}Socket client;PrintWriter writer;BufferedReader reader;TextView JChat=(TextView) findViewById(R.id.Chat);public boolean connectToServer() {try {JChat.setMovementMethod(new ScrollingMovementMethod());//Ip vom Serverclient=new Socket("127.0.0.1", 5555);reader=new BufferedReader(new InputStreamReader(client.getInputStream()));writer=new PrintWriter(client.getOutputStream());appendTextMessages("Netzwerkverbindung hergestellt");return true;} catch(Exception e) {appendTextMessages("Netzwerkverbindung konnte nicht hergestellt werden");e.printStackTrace();return false;}}class MessagesFromServerListener implements Runnable {@Overridepublic void run() {String message;try {while ((message=reader.readLine()) !=null)appendTextMessages(message);} catch(IOException e){appendTextMessages("Nachricht konnte nicht empfangen werden!");e.printStackTrace();}}}public void appendTextMessages(String message) {JChat.setText(message + "\n");}}

But the program stopped in the first few seconds with the error:

E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.example.josef.tvb_chat, PID: 4482java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.josef.tvb_chat/com.example.josef.tvb_chat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object referenceat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)at android.app.ActivityThread.-wrap11(ActivityThread.java)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:148)at android.app.ActivityThread.main(ActivityThread.java:5417)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object referenceat android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200)at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)at com.example.josef.tvb_chat.MainActivity.<init>(MainActivity.java:67)at java.lang.Class.newInstance(Native Method)at android.app.Instrumentation.newActivity(Instrumentation.java:1067)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I know I should totally buy a book about android programming, but at first I hope for somebody who would help me.:(

share|improve this question

closed as off-topic by MrSmith42, Koitoer, leepowers, TomServo, Dmitry Brant Feb 14 at 22:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – MrSmith42, Koitoer, leepowers, TomServo, Dmitry Brant
If this question can be reworded to fit the rules in the help center, please edit the question.

  • the error tells you why you have an error. you have a null pointer exception. do you know what the means? should be a pretty easy fix.– DroiDevFeb 14 at 19:54
  • This is going to be a problem TextView JChat=(TextView) findViewById(R.id.Chat);. It needs to be inside of a method. See this answer of mine to understand why.– codeMagicFeb 14 at 20:03
  • Yes I know, what it means. But I don't see where the error is:( I have looked for it nearly 3 hours– AutopferdFeb 14 at 20:04
  • Oh thanks a Lot:)– AutopferdFeb 14 at 20:08
  • Looks like you need to move your onCreate end bracket four statements down.– Ryan PierceFeb 14 at 21:19