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
 
    