0

I keep getting a null pointer exception for a linearlayout object that i defined on xml. The object was added to the layout that i set in the setcontentview() method. The linearlayout shows on the screen. I also apply an object animator to the linearlayout object and it moves as it should on the screen. I get the null pointer exception when i try to compute a calculation in a for loop with the getX() and getY() values of this xml linearlayout. This for loop resides inside an anonymous thread which is initialized inside a method. Well, here's the code. If you see the error please let me know:

LinearLayout xml_linearlayout
//inside onCreate
xml_linearlayout = (LinearLayout)getLayoutInflater.inflate(R.layout.mm,null);//i think here's problem
//here is my main layout called main. Its a relative layout
//this main layout was created programmatically not by xml format
main.addView(xml_linearlayout,parameters that have MATCH_PARENT= width and height);
//onCreate has ended
//method that attempts to use getX() and getY() of xml_linearlayout
public void methodCal(final float x, final float y){
float newX = x;
float newY = y;
new Thread(new Runnable(){
public void run(){
for(int i=0; i<30 ; i++){
double e = Math.pow(newX-xml_linearlayout.getX(),2.0);// NPE thrown here
}
} 
}).start();
}
i_o
  • 777
  • 9
  • 25
  • Post the exception please. – earthw0rmjim Jun 12 '16 at 03:27
  • java.lang.NullPointerException: Attempt to invoke virtual method 'float android.widget.LinearLayout.getX()' on a null object reference at com.g.gridview.GridClass$33.run(GridClass.java:4537)@user13 – i_o Jun 12 '16 at 03:28
  • Not sure you have visibility to newX-xml_linearlayout within your thread. – Chewy Jun 12 '16 at 03:30
  • well i did try this before : double e =Math.pow(newX,2.0); and no null pointer exception was thrown. So there's visibility for newX inside thread. The problem I think lies with xml_linearlayout @Chewy – i_o Jun 12 '16 at 03:32
  • May be you are trying to write `xml_linearlayout.getX()` . – Shree Krishna Jun 12 '16 at 03:59
  • what do you mean trying to write? @ShreeKrishna – i_o Jun 12 '16 at 04:01
  • I didn't see any initialization and declaration of `newX-xml_linearlayout`. so – Shree Krishna Jun 12 '16 at 04:10
  • Where do you call this Method? I think that's the source of your problem. Before calling the method do a null check on the linear layout and appropriately log error messages. – Sourav Kanta Jun 12 '16 at 04:55
  • Also as you are accessing the layout via a thread if your activity is closed before the Thread ends you may get a NPE. – Sourav Kanta Jun 12 '16 at 04:59
  • This method lies inside the class that has onCreate() . This method lies outside of onCreate is not inside an inner class. Now, the method gets called from another class in the package. This method gets called due to a button being pressed the button overrides onTouch and I use a simpleGestureListener to calculate onFling() criteria. So inside the class that implements the simpleGestureListener there's the overriden onFling() method. Inside here is where i call the method that causes problems. In order to call it,i create an object of my Activity which calls it.@SouravKanta – i_o Jun 12 '16 at 05:34
  • I will probably have to redesign the logic behind the animation – i_o Jun 12 '16 at 05:39

0 Answers0