Click here to Skip to main content
15,886,796 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
problem when i hit connect button then it entered into enableconnection function but not responding in setupiostream function.... please verify code

public class ClientManager extends Fragment implements OnClickListener {

private View view;
private EditText ipBox, msgbox;
private ToggleButton connBtn;
private Button sendBtn;
private TextView txtView;
private Socket client;
private DataOutputStream out;
private BufferedReader in;
private TextView switchStatus;
private Switch mySwitch;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.client_manager, container, false);
setUpAllViews();
SetSwitch();
return view;
}



void setUpAllViews()
{
ipBox = (EditText) view.findViewById(R.id.ipBox);
msgbox = (EditText) view.findViewById(R.id.msgBox);
connBtn = (ToggleButton) view.findViewById(R.id.connect);
sendBtn = (Button) view.findViewById(R.id.send);
txtView = (TextView) view.findViewById(R.id.text);

connBtn.setOnClickListener(this);
}

private void SetSwitch()
{
switchStatus = (TextView) view.findViewById(R.id.switchStatus);
mySwitch = (Switch) view.findViewById(R.id.mySwitch);

mySwitch.setChecked(true);
//attach a listener to check for changes in state
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

if(isChecked){
switchStatus.setText("Switch is ON");
}else{
switchStatus.setText("Switch is OFF");
}

}
});

//check the current state before we display the screen

}


@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.connect:
if(connBtn.isChecked())
{
enableConnection();
}
else
{
/// Toast.makeText(this, "Disabling", Toast.LENGTH_SHORT).show();
disableConnection();
}
break;
case R.id.send:
sendDataOverCommunication();
break;
case R.id.mySwitch:
if(mySwitch.isChecked()){
switchStatus.setText("ON");
}
else {
switchStatus.setText("OFF");
}
break;

}

}

private void setValues(int id, boolean value)
{
switch(id)
{
case R.id.ipBox: ipBox.setEnabled(value); break;
case R.id.msgBox: msgbox.setEnabled(value); break;
case R.id.send: sendBtn.setEnabled(value); break;
case R.id.connect: connBtn.setChecked(value); break;
case R.id.mySwitch: switchStatus.setEnabled(value);break;
}
}

private void setUpIOStreams()
{
try
{
getActivity().runOnUiThread(new Runnable(){

@Override
public void run() {
try {
Log.d("Before Socket assigned: ", client.toString());
InetAddress addr = InetAddress.getByName(ipBox.getText().toString());
//Thread.sleep(3000);
client = new Socket(addr,5800);
Log.d("Socket: ", client.toString());
if(client.isConnected())
{
Log.d("TAaaaaaaG 1: ", "true");
out = new DataOutputStream(client.getOutputStream());
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
}
else
{
client.close();
Log.d("TAaaaaaaG 2: ", "false");
}

}
/*catch (InterruptedException e)
{
disableConnection();
e.printStackTrace();
} */
catch(UnknownHostException eux)
{
disableConnection();
eux.printStackTrace();
}
catch(IOException ex)
{
disableConnection();
ex.printStackTrace();
}
catch (Exception e) {
disableConnection();
e.printStackTrace();
}
}
});

}

catch(Exception e)
{
Log.e("error", e.getMessage());
disableConnection();
}
}

private void enableConnection()
{
try
{
setUpIOStreams();
if(client.isConnected())
{
setValues(R.id.connect,true);
setValues(R.id.send,true);
setValues(R.id.ipBox,false);
setValues(R.id.msgBox,true);
sendBtn.setOnClickListener(this);
}
}


catch(Exception e)
{
Log.e("exce:", Log.getStackTraceString(e));
Log.e("Set UI thread: ", "exception setupIOStrea" + e.getMessage() + e.getStackTrace());
}
}

private void disableConnection()
{
if(client != null)
{
try
{
client.close();
}
catch(Exception e)
{

}
setValues(R.id.connect,false);
setValues(R.id.ipBox,true);
setValues(R.id.msgBox,false);
setValues(R.id.send,false);
}
else
{
setValues(R.id.connect,false);
}
}

private void sendDataOverCommunication()
{
getActivity().runOnUiThread(new Runnable() {
@Override
public void run(){
String sentence = "";

try
{
if(client.isClosed())
setUpIOStreams();
if(msgbox.getText() != null)
{
sentence = msgbox.getText().toString() + switchStatus.getText().toString();
}
else
{
sentence = switchStatus.getText().toString();
}
out.writeBytes(sentence);
Log.e("Sent ", sentence);
sentence = in.readLine();
msgbox.setText(sentence);
Log.e("Received ", sentence);

out.flush();
out.close();
in.close();
client.close();
}
catch (UnknownHostException e) {
e.printStackTrace();
Log.e("Caused ", e.getCause().toString() + e.getMessage());
setValues(R.id.ipBox,true);
setValues(R.id.connect,false);
setValues(R.id.send,false);
setValues(R.id.msgBox,false);
} catch (IOException e) {
e.printStackTrace();
Log.e("Caused ", e.getCause().toString() + e.getMessage());
setValues(R.id.ipBox,true);
setValues(R.id.connect,false);
setValues(R.id.send,false);
setValues(R.id.msgBox,false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

}


05-19 21:55:03.473: W/System.err(3688): java.lang.NullPointerException
05-19 21:55:03.493: W/System.err(3688): at com.androidbegin.sidemenutabstutorial.ClientManager$2.run(ClientManager.java:166)
05-19 21:55:03.493: W/System.err(3688): at android.app.Activity.runOnUiThread(Activity.java:4644)
05-19 21:55:03.497: W/System.err(3688): at com.androidbegin.sidemenutabstutorial.ClientManager.setUpIOStreams(ClientManager.java:161)
05-19 21:55:03.497: W/System.err(3688): at com.androidbegin.sidemenutabstutorial.ClientManager.enableConnection(ClientManager.java:227)
05-19 21:55:03.497: W/System.err(3688): at com.androidbegin.sidemenutabstutorial.ClientManager.onClick(ClientManager.java:114)
05-19 21:55:03.497: W/System.err(3688): at android.view.View.performClick(View.java:4204)
05-19 21:55:03.497: W/System.err(3688): at android.widget.CompoundButton.performClick(CompoundButton.java:100)
05-19 21:55:03.501: W/System.err(3688): at android.view.View$PerformClick.run(View.java:17355)
05-19 21:55:03.501: W/System.err(3688): at android.os.Handler.handleCallback(Handler.java:725)
05-19 21:55:03.501: W/System.err(3688): at android.os.Handler.dispatchMessage(Handler.java:92)
05-19 21:55:03.501: W/System.err(3688): at android.os.Looper.loop(Looper.java:137)
05-19 21:55:03.501: W/System.err(3688): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-19 21:55:03.501: W/System.err(3688): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 21:55:03.501: W/System.err(3688): at java.lang.reflect.Method.invoke(Method.java:511)
05-19 21:55:03.501: W/System.err(3688): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-19 21:55:03.501: W/System.err(3688): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-19 21:55:03.501: W/System.err(3688): at dalvik.system.NativeStart.main(Native Method)
05-19 21:55:03.501: E/exce:(3688): java.lang.NullPointerException
05-19 21:55:03.501: E/exce:(3688): at com.androidbegin.sidemenutabstutorial.ClientManager.enableConnection(ClientManager.java:228)
05-19 21:55:03.501: E/exce:(3688): at com.androidbegin.sidemenutabstutorial.ClientManager.onClick(ClientManager.java:114)
05-19 21:55:03.501: E/exce:(3688): at android.view.View.performClick(View.java:4204)
05-19 21:55:03.501: E/exce:(3688): at android.widget.CompoundButton.performClick(CompoundButton.java:100)
05-19 21:55:03.501: E/exce:(3688): at android.view.View$PerformClick.run(View.java:17355)
05-19 21:55:03.501: E/exce:(3688): at android.os.Handler.handleCallback(Handler.java:725)
05-19 21:55:03.501: E/exce:(3688): at android.os.Handler.dispatchMessage(Handler.java:92)
05-19 21:55:03.501: E/exce:(3688): at android.os.Looper.loop(Looper.java:137)
05-19 21:55:03.501: E/exce:(3688): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-19 21:55:03.501: E/exce:(3688): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 21:55:03.501: E/exce:(3688): at java.lang.reflect.Method.invoke(Method.java:511)
05-19 21:55:03.501: E/exce:(3688): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-19 21:55:03.501: E/exce:(3688): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-19 21:55:03.501: E/exce:(3688): at dalvik.system.NativeStart.main(Native Method)
05-19 21:55:03.505: E/Set UI thread:(3688): exception setupIOStreanull[Ljava.lang.StackTraceElement;@53533ebc
Posted
Comments
Dominic Burford 20-May-15 3:25am    
There is far too much code here. Please take the time to step through the code in the debugger and try to see where the problem is arising, and identify the area of the code that is causing the problem. This not only helps you, but it helps us too. If you are still getting problems, then feel free to post again with a more specific questions and specific are of the code.

1 solution

05-19 21:55:03.501: E/exce:(3688): java.lang.NullPointerException
05-19 21:55:03.501: E/exce:(3688): at com.androidbegin.sidemenutabstutorial.ClientManager.enableConnection(ClientManager.java:228)
05-19 21:55:03.501: E/exce:(3688): at com.androidbegin.sidemenutabstutorial.ClientManager.onClick(ClientManager.java:114)
05-19 21:55:03.501: E/exce:(3688): at android.view.View.performClick(View.java:4204)
05-19 21:55:03.501: E/exce:(3688): at android.widget.CompoundButton.performClick(CompoundButton.java:100)

Those messages are telling you where to look in your debug session.
 
Share this answer
 

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