-1

I have no idea why I am getting a null pointer exception. This is my first time posting, it would mean the world to get any feedback. I have been researching on the internet for a while but cant point out whats wrong. I'm fairly new to object orientated programming so I would really appreciate any help with this. Thanks in advance.

Here is the full color grid class

    package au.edu.holmesglen.mswemmer.gridviewdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Random;

public class ColorGrid extends AppCompatActivity {

    GridView gridview;
    Item[] gridArray = new Item[16];
    ImageAdapter iAdapter;

    int columncount = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_grid);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        // generate 4x4 array with all items in the grid set to the grey image
        for(int i = 0; i< 16; i++){
            gridArray[i] = new Item(R.drawable.grey, "grey");
            Log.v("MyApp","GRID CREATED!!!!!");
        }

        //use the ImageAdapter to pass the array to the GridView object
        GridView grid = (GridView) findViewById(R.id.gridview);
        iAdapter = new ImageAdapter(this, gridArray);
        grid.setAdapter(iAdapter);

        //everytime an Item is clicked call the nextColor method to change to a different image
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                int x = position / columncount;
                int y = position % columncount;

                int c = gridArray[position].nextColor();
                checkForWin();
                ((ImageView)v).setImageResource(c);

                Toast.makeText(getApplicationContext(), x + " " + y,
                        Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void checkForWin() {
        Log.d("MyApp","HELLO!!!!!");
        for (int i = 0; i < 16; i += 4) {

            // Check for blue
            if ((gridArray[i].getColor() == R.drawable.blue
                    && gridArray[i + 1].getColor() == R.drawable.blue
                    && gridArray[i + 2].getColor() == R.drawable.blue)
                    || (gridArray[i + 1].getColor() == R.drawable.blue
                    && gridArray[i + 2].getColor() == R.drawable.blue
                    && gridArray[i + 3].getColor() == R.drawable.blue))
            {
                Toast.makeText(getApplicationContext(),
                        "THREE WHITE IN A ROW HORIZONTALLY", Toast.LENGTH_SHORT)
                        .show();
                Log.d("MyApp", "3 in row horizontally BLUE");
            }

            // Check for green
            if ((gridArray[i].getColor() == R.drawable.green
                    && gridArray[i + 1].getColor() == R.drawable.green
                    && gridArray[i + 2].getColor() == R.drawable.green)
                    || (gridArray[i + 1].getColor() == R.drawable.green
                    && gridArray[i + 2].getColor() == R.drawable.green
                    && gridArray[i + 3].getColor() == R.drawable.green))
            {
                Toast.makeText(getApplicationContext(),
                        "THREE RED IN A ROW HORIZONTALLY", Toast.LENGTH_SHORT)
                        .show();
                Log.d("MyApp", "3 in row horizontally GREEN");
            }

        }

        // Check for vertical loss
        for (int i = 0; i <= 7; i++) {
            /*
             * This for loop is looping through each column and checking if 3 drawables are in a row
             */

            //Check for blue loss
            if (gridArray[i].getColor() == R.drawable.blue
                    && gridArray[i + 4].getColor() == R.drawable.blue
                    && gridArray[i + 8].getColor() == R.drawable.blue)
            {
                Toast.makeText(getApplicationContext(),
                        "THREE WHITE IN A ROW VERTICALLY", Toast.LENGTH_SHORT).show();
                Log.d("MyApp", "3 in row vertically BLUE");
            }

            //Check for green loss
            if (gridArray[i].getColor() == R.drawable.green
                    && gridArray[i + 4].getColor() == R.drawable.green
                    && gridArray[i + 8].getColor() == R.drawable.green) {
                Toast.makeText(getApplicationContext(),
                        "THREE RED IN A ROW VERTICALLY", Toast.LENGTH_SHORT).show();
                Log.d("MyApp", "3 in row vertically GREEN");
            }
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_color_grid, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

And here is my log

08-23 18:00:46.219 7772-7772/au.edu.holmesglen.mswemmer.gridviewdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: au.edu.holmesglen.mswemmer.gridviewdemo, PID: 7772
    java.lang.RuntimeException: Unable to start activity ComponentInfo{au.edu.holmesglen.mswemmer.gridviewdemo/au.edu.holmesglen.mswemmer.gridviewdemo.ColorGrid}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2439)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
        at android.app.ActivityThread.access$800(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5633)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
        at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
        at au.edu.holmesglen.mswemmer.gridviewdemo.ColorGrid.onCreate(ColorGrid.java:45)
        at android.app.Activity.performCreate(Activity.java:5312)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
        at android.app.ActivityThread.access$800(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:157) 
        at android.app.ActivityThread.main(ActivityThread.java:5633) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712) 
        at dalvik.system.NativeStart.main(Native Method) 
MrSwemmer
  • 1
  • 2
  • post relevant code from ColorGrid.java:45 – Abbas Aug 23 '17 at 08:20
  • 1
    @SatanPandeya The link posted by you asks about null pointer exception in generic sense. This question is specific to given use case. I fail to see how this is a duplicate. – mahoriR Aug 23 '17 at 08:56

2 Answers2

1

Edited

@MrSwemmer have you double checked that the id of gridview is same as put here?.it can be the id of another gridview (if present in another layout)

Error is from the activity ColorGrid.class please see the code there on the line 45. or post the code here for activity

Umar Hussain
  • 3,461
  • 1
  • 16
  • 38
  • Thanks for getting back to me, I updated it, to see the full class. Line 45 is :grid.setAdapter(iAdapter); – MrSwemmer Aug 23 '17 at 08:33
  • grid.setAdapter(iAdapter) is line 45, thanks, been scratching my head over this for a while – MrSwemmer Aug 23 '17 at 08:41
  • Still have no clue what the problem is, why would the adapter be causing a null exception – MrSwemmer Aug 23 '17 at 08:47
  • @MrSwemmer have you double checked that the id of gridview is same as put here?.it can be the id of another gridview (if present in another layout) – Umar Hussain Aug 23 '17 at 09:11
  • 1
    IT WORKS!!! Thank you, you guys were right! I was referencing a grid in a different xml file. Im sorry Im new to this, Thank you so much for your help – MrSwemmer Aug 23 '17 at 09:27
  • please accept my answer then, I have updated it with correct answer. – Umar Hussain Aug 23 '17 at 09:31
0

Based on comment from @MrSwemmer, the problematic line is grid.setAdapter(iAdapter). Please check GridView grid = (GridView) findViewById(R.id.gridview); and make sure a view id = "gridview" is actually present.

Add following debug code to verify before line 45: if(grid==null) Log.d("GRID","NULL); else if(iAdapter==null)Log.d("Adapter","null"); else Log.d("All","Good");

mahoriR
  • 4,377
  • 3
  • 18
  • 27
  • 1
    android:id="@+id/gridview" thanks for looking at this, the id is "gridview" that's not the problem. – MrSwemmer Aug 23 '17 at 08:44
  • Could you just put following code right before line 45 and see results ` if(grid==null) Log.d("GRID","NULL); else if(iAdapter==null)Log.d("Adapter","null"); else Log.d("All","Good");` – mahoriR Aug 23 '17 at 08:49
  • @MrSwemmer Please accept this as right answer. Thanks. – mahoriR Aug 23 '17 at 12:20