0

I have a global variable I initializes in my app context to make it accessible to all classes.

In one screen I have a list with an adapter, where I want to access the variable called "horizontalBoxPosition",

here is how I get the data

String horitonalBoxPosition, verticalBoxPosition;

        horitonalBoxPosition = ((appContext) getContext()).getHorizontalBoxPosition();

But it outputs the error:

Attempt to invoke virtual method 'java.lang.String apps.radwin.wintouch.appContext.getHorizontalBoxPosition()' on a null object reference

How can this be fixed ? Tried many things suggested but they didn't work out.

Here is my full adapter:

package apps.radwin.wintouch.adapters;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import apps.radwin.wintouch.Aligment_Scan_Activity;
import apps.radwin.wintouch.R;
import apps.radwin.wintouch.appContext;
import apps.radwin.wintouch.models.AligmentListModel;

/**
 * Created by shay_v on 04/05/2016.
 */
public class AligmentScanListAdapter extends ArrayAdapter<AligmentListModel> {

    public AligmentScanListAdapter(Context context, ArrayList<AligmentListModel> aligmentModel) {
        super(context, 0, aligmentModel);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        AligmentListModel aligmentModelData = getItem(position);

        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_layout_aligment_scan, parent, false);
        }
        // Lookup view for data population
//        TextView spliceNumberTextVew = (TextView) convertView.findViewById(R.id.list_layout_splice_textView); //casting
//
//        TextView scannedUpTextView = (TextView) convertView.findViewById(R.id.list_layout_scanned_up_textView); //casting
//        TextView scannedNormalTextView = (TextView) convertView.findViewById(R.id.list_layout_scanned_normal_textView); //casting
//        TextView scannedButtomTextView = (TextView) convertView.findViewById(R.id.list_layout_scanned_buttom_textView); //casting
//
//        TextView baseStationUpTextView = (TextView) convertView.findViewById(R.id.list_layout_base_up_textView); //casting
//        TextView baseStationNormalTextView = (TextView) convertView.findViewById(R.id.list_layout_base_normal_textView); //casting
//        TextView baseStationButtomTextView = (TextView) convertView.findViewById(R.id.list_layout_base_buttom_textView); //casting

        //Log.d ("myLogs", "drawing list");
        //lookup the views to change color later
        View leftSquare = (View) convertView.findViewById(R.id.aligmentList_LeftSquare);
        View middleSquare = (View) convertView.findViewById(R.id.aligmentList_middleSquare);
        View rightSquare = (View) convertView.findViewById(R.id.aligmentList_RightSquare);


        View leftSquareFrame = (View) convertView.findViewById(R.id.aligmentList_LeftSquare_frame);
        View middleSquareFrame = (View) convertView.findViewById(R.id.aligmentList_middleSquare_frame);
        View rightSquareFrame = (View) convertView.findViewById(R.id.aligmentList_RightSquare_frame);


        // Populate the data into the template view using the data object
//        spliceNumberTextVew.setText(aligmentModelData.cellNumber);
//
//        scannedUpTextView.setText(aligmentModelData.sectorsScannedHigh);
//        scannedNormalTextView.setText(aligmentModelData.sectorsScannedMedium);
//        scannedButtomTextView.setText(aligmentModelData.sectorsScannedLow);
//
//        baseStationUpTextView.setText(aligmentModelData.basePointsRecivedHigh);
//        baseStationNormalTextView.setText(aligmentModelData.basePointsRecivedMedium);
//        baseStationButtomTextView.setText(aligmentModelData.basePointsRecivedLow);

        if (aligmentModelData.sectorsScannedHigh.matches("true")) { //colors the left colum, the hight one
            leftSquare.setBackgroundColor(Color.argb(255, 0, 204, 102));
        } else {
            leftSquare.setBackgroundColor(Color.argb(255, 244, 224, 224));
        }

        if (aligmentModelData.sectorsScannedMedium.matches("true")) { //colors the middle colum, the hight one
            middleSquare.setBackgroundColor(Color.argb(255, 0, 204, 102));
        } else {
            middleSquare.setBackgroundColor(Color.argb(255, 244, 224, 224));
        }

        if (aligmentModelData.sectorsScannedLow.matches("true")) { //colors the low colum, the hight one
            rightSquare.setBackgroundColor(Color.argb(255, 0, 204, 102));
        } else {
            rightSquare.setBackgroundColor(Color.argb(255, 244, 224, 224));
        }


        /////////////
        //color the base stations
        int basePointsHighLength = aligmentModelData.basePointsRecivedHigh.toString().length(); // colors the rest of your basepoints found
        int basePointsRecivedMedium = aligmentModelData.basePointsRecivedMedium.toString().length();
        int basePointsRecivedLow = aligmentModelData.basePointsRecivedLow.toString().length();

        if (basePointsHighLength > 2) { //colors the left colum, the hight one
            leftSquare.setBackgroundColor(Color.argb(255, 255, 51, 0));
        }

        if (basePointsRecivedMedium > 2) { //colors the middle colum, the hight one
            middleSquare.setBackgroundColor(Color.argb(255, 255, 51, 0));
        }

        if (basePointsRecivedLow > 2) { //colors the low colum, the hight one
            rightSquare.setBackgroundColor(Color.argb(255, 255, 51, 0));
        }


        //removing the background frame
        String horitonalBoxPosition, verticalBoxPosition;

        horitonalBoxPosition = ((appContext) getContext()).getHorizontalBoxPosition();

        Log.d ("myLogs", "horizontal Position"+horitonalBoxPosition);


        return convertView;

    }


}

And my full context class:

package apps.radwin.wintouch;

import android.app.Application;
import android.util.Log;

import apps.radwin.wintouch.screenManagers.AligmentManager;

/**
 * Created by shay_v on 16/05/2016.
 */

//////////////////////////////////////////////////////////////////////////////
//THIS IS THE APP CONTEXT OF THE APPLICATION - COUTION REQUIRED - HDNLE GENTLY
//////////////////////////////////////////////////////////////////////////////


public class appContext extends Application {

    private AligmentManager aligmentManagerVar = new AligmentManager(); // making aligment Manager accecible to all classes

    private String horizontalBoxPosition = "0";
    private String verticalBoxPosition = "0";


    public String getVerticalBoxPosition() {
        Log.d ("myLogs", "getsVertical: "+verticalBoxPosition);
        return verticalBoxPosition;
    }

    public void setVerticalBoxPosition(String verticalBoxPosition) {
        Log.d ("myLogs", "setsVertical: "+verticalBoxPosition);
        this.verticalBoxPosition = verticalBoxPosition;
    }

    public String getHorizontalBoxPosition() {
        return horizontalBoxPosition;
    }

    public void setHorizontalBoxPosition(String horizontalBoxPosition) {
        this.horizontalBoxPosition = horizontalBoxPosition;
    }

    public AligmentManager getAligmentManagerVar() {
        return aligmentManagerVar;
    }






}

full error LOG:

06-02 11:12:08.091 4480-4480/apps.radwin.wintouch D/AndroidRuntime: Shutting down VM
06-02 11:12:08.101 4480-4480/apps.radwin.wintouch E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: apps.radwin.wintouch, PID: 4480
                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String apps.radwin.wintouch.appContext.getHorizontalBoxPosition()' on a null object reference
                                                                        at apps.radwin.wintouch.adapters.AligmentScanListAdapter.getView(AligmentScanListAdapter.java:114)
                                                                        at android.widget.AbsListView.obtainView(AbsListView.java:2929)
                                                                        at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
                                                                        at android.widget.ListView.onMeasure(ListView.java:1212)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
                                                                        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:664)
                                                                        at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                                                                        at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1149)
                                                                        at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:729)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
                                                                        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:695)
                                                                        at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:158)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120
Chief Madog
  • 1,738
  • 4
  • 28
  • 55
  • Declare all the methods of your appContext as static so that you don't have to instantiate it globally and call appContext.horitonalBoxPosition() directly in your adapter – mgcaguioa Jun 02 '16 at 08:34
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin Jun 02 '16 at 08:45
  • first post full logcat log, second it is not possible to get NPE with this code ... as you should get NPE in line `convertView = LayoutInflater.from(getContext())...` first ... (why? it is logical and obvious: `horitonalBoxPosition = ((appContext) getContext()).getHorizontalBoxPosition();` the only reason for NPE here is `getContext() == null` ... but you use `getContext()` before to inflate the view ... so it is not possible) – Selvin Jun 02 '16 at 08:53
  • +Selvin i added the full log – Chief Madog Jun 02 '16 at 10:14

2 Answers2

0

as +mgcaguioa for replying to change everything to static worked like a charm ! thnx

for future users here is the new appcontext to show you what i did:

package apps.radwin.wintouch;

import android.app.Application;

import apps.radwin.wintouch.screenManagers.AligmentManager;

/**
 * Created by shay_v on 16/05/2016.
 */

//////////////////////////////////////////////////////////////////////////////
//THIS IS THE APP CONTEXT OF THE APPLICATION - COUTION REQUIRED - HDNLE GENTLY
//////////////////////////////////////////////////////////////////////////////


public class appContext extends Application {

    private AligmentManager aligmentManagerVar = new AligmentManager(); // making aligment Manager accecible to all classes

    private static String horizontalBoxPosition;
    private static String verticalBoxPosition;

    public static String getVerticalBoxPosition() {
        return verticalBoxPosition;
    }

    public static void setVerticalBoxPosition(String verticalBoxPosition) {
        verticalBoxPosition = verticalBoxPosition;
    }

    public static String getHorizontalBoxPosition() {
        return horizontalBoxPosition;
    }

    public static void setHorizontalBoxPosition(String horizontalBoxPosition) {
        horizontalBoxPosition = horizontalBoxPosition;
    }

    public AligmentManager getAligmentManagerVar() {
        return aligmentManagerVar;
    }



}
Chief Madog
  • 1,738
  • 4
  • 28
  • 55
-1

try to use this constructor for your 'AligmentScanListAdapter ' class:

public Context con;
public AligmentScanListAdapter(Context context, ArrayList<AligmentListModel> aligmentModel) {
    super(context, 0, aligmentModel);
    this.con=context;
}

and after that use the code bellow instead of your removing the background frame code:

 //removing the background frame
    String horitonalBoxPosition, verticalBoxPosition;

    horitonalBoxPosition = this.con.getHorizontalBoxPosition();

    Log.d ("myLogs", "horizontal Position"+horitonalBoxPosition);


    return convertView;
pouyan
  • 3,445
  • 4
  • 26
  • 44
  • i did not understand what you mean. i just wanted to point that the thing that caused Error is that it's context become null. and if he/she save the context in constructor it can be solved. – pouyan Jun 02 '16 at 08:40
  • Ok, man i did not say that i am sure that i am right. i am just trying to help. but still i did not found "mContext" field in above code :(! – pouyan Jun 02 '16 at 08:46
  • because it is in the base class ... did you notice that there are 2 links in my first comment ... click the second one(under ArrayAdapter) – Selvin Jun 02 '16 at 08:47
  • yes you are right and i am sorry for that. i try to think more and edit my answer in better way. – pouyan Jun 02 '16 at 08:49