Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code and I didn't know how can I solve it
when catching the error tell me
Java.Lang.NullPointerException 

Spatially in error3


Java
package com.app.ServerClient;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import java.io.BufferedWriter;
import java.io.IOException;
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.os.Bundle;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;

public class ServerClient extends Activity {
		private Button bt;
	   private TextView tv;
	   private Socket socket;
	   private String serverIpAddress = "10.0.2.2";
	   // AND THAT'S MY DEV'T MACHINE WHERE PACKETS TO
	   // PORT 5000 GET REDIRECTED TO THE SERVER EMULATOR'S
	   // PORT 6000
	   private static final int REDIRECTED_SERVERPORT = 5000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		 bt = (Button) findViewById(R.id.myButton);
	      tv = (TextView) findViewById(R.id.myTextView);
	      try {
	         InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
	         socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
	      } catch (UnknownHostException e1) {
	         e1.printStackTrace();
	      } catch (IOException e1) {
	         e1.printStackTrace();
	      }
	      bt.setOnClickListener(new OnClickListener() {
	         public void onClick(View v) {
	            try {
	               EditText et = (EditText) findViewById(R.id.EditText01);
	               String str = et.getText().toString();
	               PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
	               out.println(str);
	               Log.d("Client", "Client sent message");
	            } catch (UnknownHostException e) {
	               tv.setText("Error1");
	               e.printStackTrace();
	            } catch (IOException e) {
	               tv.setText("Error2");
	               e.printStackTrace();
	            } catch (Exception e) {
	               tv.setText("Error3");
	               Toast.makeText(getBaseContext(), e+"", Toast.LENGTH_LONG).show();
	            }
	         }
	      });

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.server_client, menu);
		return true;
	}

}
Posted
Updated 17-Aug-13 1:54am
v2

1 solution

There is a good chance that the findViewById[^] function is returning null.
Debug the code to check what the function returns.
 
Share this answer
 
Comments
Ammar Al-hamdabni 17-Aug-13 8:19am    
Actually I asked about wifi how to send data and thay gave that code however I didn't know what the error means
specitally when I click the button (error3).
For the webpag you sent me I studied before.

Thank you Mr.Andre kraak
Richard MacCutchan 17-Aug-13 12:03pm    
Go and talk to the people who gave you the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900