0

I am getting the following stacktrace:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference --------- Stack trace --------- mypackage.Inbox$9.onclick(Inbox.java:1538) android.view.View.performClick(View.java:5204) android.view.View$PerformClick.run(View.java:21153) android.os.Handler.handleCallback(Handler.java:739) android.os.Handler.dispatchMessage(Handler.java:95) android.os.Looper.loop(Looper.java:148) android.app.ActivityThread.main(ActivityThread.java:5444) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)

double taxvalue = 0.0;
double acttotal=Global.getDouble(((EditText)container.findViewById(R.id.textView9)).getText().toString());
taxvalue = Global.getDouble(((EditText) container.findViewById(R.id.textView8)).getText().toString());

((EditText) (table.getChildAt(1).findViewById(R.id.amount))).setText(Global.formatDouble(acttotal - taxvalue));

Here table - LinearLayout, Last line is Line no 1538.

I tried table=null; But cannot be reproduced. what is the chance to get this issue.

balazs630
  • 3,421
  • 29
  • 46
Skyvin
  • 3
  • 3
  • 'void android.widget.EditText.setText(java.lang.CharSequence)' is null – MVCNoob Nov 30 '16 at 07:37
  • setText will also accept numbers as it is inputs then search in R.id. You should try to turn numbers to strings before using it inside setText method. – Orkun Kocyigit Nov 30 '16 at 07:38
  • Hi sushildlh, thanks for your reply. but i am not asking what is nullpointer exception.. from my code sometimes getting nullpointerexception but i can't reproduce these. sometime my users are getting this thats why i am asking – Skyvin Nov 30 '16 at 07:38
  • This looks problematic: `table.getChildAt(1).findViewById(R.id.amount)`. The child you're getting with `getChildAt` is probably the `EditText`. Either remove `getChildAt(1)` or remove `findViewById` – 0xDEADC0DE Nov 30 '16 at 07:39
  • Hi Orkun, do you think it will throw nullpointer exception due to String - numbers change? – Skyvin Nov 30 '16 at 07:39
  • Hi 0xDEADC0DE, as you said. but why it is not reproduces the issue all times – Skyvin Nov 30 '16 at 07:41
  • `findViewById(R.id.amount)` returns `null` that's the reason behind your NPE. – Droidman Nov 30 '16 at 07:41
  • It seems that R.id.amount is null. Check if the table really contains that edittext. Bear in mind that Java is mainly 0-indexed, so try getChildAt(0). I have little experience with android but give that a try – 7H3_H4CK3R Nov 30 '16 at 07:41
  • while casting a view (taken by 'getChildAt()' method) you must check with 'instanceof' operator first.. – Mahesh B ツ Nov 30 '16 at 08:01

1 Answers1

1

There Are two possible reasons:

1.Your Data May be null which you set on Edit text please debug your data

2.First Initialize your Edit Text field in OnCreate like

EditText edittext=(EditText).findViewById(R.id.Yourid);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Ravish Sharma
  • 207
  • 2
  • 14