0

Can someone help me? I keep getting this NullPointerException error.

03-23 22:50:43.519: E/AndroidRuntime(1978): java.lang.NullPointerException
03-23 22:50:43.519: E/AndroidRuntime(1978):     at         android.graphics.Bitmap.checkPixelsAccess(Bitmap.java:1365)
03-23 22:50:43.519: E/AndroidRuntime(1978):     at  android.graphics.Bitmap.getPixels(Bitmap.java:1310)
03-23 22:50:43.519: E/AndroidRuntime(1978):     at com.example.ocrtry.Entry2.downSample(Entry2.java:227)
03-23 22:50:43.519: E/AndroidRuntime(1978):     at com.example.ocrtry.MainActivity$1.onClick(MainActivity.java:42)

I tried putting a try and catch statement at downSample and i dont even get an output. Here is the code for downSample

public void downSample()
{
  int w = bmInput.getWidth();
  int h = bmInput.getHeight();
  int[] pixels = null;
  bmInput.getPixels(pixels, 0, w, 0, 0, w, h);
  pixelMap = (int[])pixels;
  findBounds(w, h);

  SampleData data = sample.getData();

  ratioX = (double) (downSampleRight - downSampleLeft)
  / (double) data.getWidth();
  ratioY = (double) (downSampleBottom - downSampleTop)
  / (double) data.getHeight();

  for (int y = 0; y < data.getHeight(); y++)
  {
     for (int x = 0; x < data.getWidth(); x++)
     {
     if (downSampleQuadrant(x, y))
       data.setData(x, y, true);
     else
       data.setData(x, y, false);
     }
 }
}

1 Answer 1

3

The problem is downSample() calls

bmInput.getPixels(pixels, 0, w, 0, 0, w, h);

with a null pixels array.

You need to allocate the array, and make it a suitable size.

int[] pixels = new int[w * h];

How you can debug a case like this: The stack trace shows the problem detected within Bitmap.getPixels() called from downSample(). That means the relevant line of your code is where it calls getPixels() (you can also tell by the line number Entry2.java:227 in the stack trace). If bmInput was null it would throw a NPE trying to call a getPixels() method. (Actually, it would've thrown a NPE on the first line of the method, trying to call bmInput.getWidth().) So pixels is the only remaining value that could be null. Then looking one line earlier in the code, it sets bmInput to null.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.