1

I'm currently converting a Java program to an Android program and I keep having this error. I'm trying to convert it for the past few days and I'm stuck here

log:

03-24 01:18:21.289: E/AndroidRuntime(2329): FATAL EXCEPTION: main
03-24 01:18:21.289: E/AndroidRuntime(2329): Process: com.example.ocrtry, PID: 2329
03-24 01:18:21.289: E/AndroidRuntime(2329): java.lang.NullPointerException
03-24 01:18:21.289: E/AndroidRuntime(2329):     at com.example.ocrtry.Entry2.downSample(Entry2.java:231)
03-24 01:18:21.289: E/AndroidRuntime(2329):     at com.example.ocrtry.MainActivity$1.onClick(MainActivity.java:42)

Line 231: data = sample.getData();

here is a code snippet of Entry2.downSample:

protected Sample sample;
public void downSample()
{
  int w = bmInput.getWidth();
  int h = bmInput.getHeight();
  int[] pixels = new int[bmInput.getWidth() * bmInput.getHeight()];
  bmInput.getPixels(pixels, 0, w, 0, 0, w, h);
  pixelMap = (int[])pixels;
  findBounds(w, h);
  SampleData data;
  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);
     }
  }
}

and here is the code of Sample class:

public class Sample
{

  //The image data.
  SampleData data;
  /**
  * The constructor.
  * 
  * @param width
  *          The width of the downsampled image
  * @param height
  *          The height of the downsampled image
  */
  Sample(int width, int height)
  {
    data = new SampleData(' ', width, height);
  }

 /**
 * The image data object.
 * 
 * @return The image data object.
 */
  SampleData getData()
  {
    return data;
  }

 /**
 * Assign a new image data object.
 * 
 * @param data
 *          The image data object.
 */
  void setData(SampleData data)
  {
    this.data = data;
  } 
}

the code for SampleData:

public class SampleData
{

  //The downsampled data as a grid of booleans.
  protected boolean grid[][];

  protected char letter;

  /**
  * The constructor
  * 
  * @param letter
  *          What letter this is
  * @param width
  *          The width
  * @param height
  *          The height
  */
 public SampleData(char letter, int width, int height)
  {
    grid = new boolean[width][height];
    this.letter = letter;
  }

  /**
  * Set one pixel of sample data.
  * 
  * @param x
  *          The x coordinate
  * @param y
  *          The y coordinate
  * @param v
  *          The value to set
  */
  public void setData(int x, int y, boolean v)
  {
    grid[x][y] = v;
  }

  /**
  * Get a pixel from the sample.
  * 
  * @param x
  *          The x coordinate
  * @param y
  *          The y coordinate
  * @return The requested pixel
  */
  public boolean getData(int x, int y)
  {
    return grid[x][y];
  }

  public void clear()
  {
    for (int x = 0; x < grid.length; x++)
      for (int y = 0; y < grid[0].length; y++)
        grid[x][y] = false;
  }
  public int getHeight()
  {
    return grid[0].length;
  }
  public int getWidth()
  {
    return grid.length;
  }
  public char getLetter()
  {
    return letter;
  }
  public void setLetter(char letter)
  {
    this.letter = letter;
  }
  public int compareTo(Object o)
  {
    SampleData obj = (SampleData) o;
    if (this.getLetter() == obj.getLetter())
      return 0;
    else if (this.getLetter() > obj.getLetter())
      return 1;
    else
      return -1;
  }

  public boolean equals(Object o)
  {
    return (compareTo(o) == 0);
  }
  public String toString()
  {
    return "" + letter;
  }
  public Object clone()

  {
    SampleData obj = new SampleData(letter, getWidth(), getHeight());
    for (int y = 0; y < getHeight(); y++)
      for (int x = 0; x < getWidth(); x++)
        obj.setData(x, y, getData(x, y));
    return obj;
  }

}
  • what is Line 231 in your Entry2.java ? – AndyN Mar 24 '14 at 05:41
  • 1
    If this regarding scaling down the image to display it in android then I think you will get your answer [here](http://stackoverflow.com/questions/21229917/android-capture-and-display-image-app) and please try to put some more to your question so that readers will get the overview of what exactly your question is and what you want....thnx – Yogesh D Mar 24 '14 at 05:44
  • Line 231 is: data = sample.getData(); – DreiDreiDrei Mar 24 '14 at 05:44
  • @DreiDreiDrei, it is because you havent create its instance. – Lazy Ninja Mar 24 '14 at 05:46

3 Answers3

1
  SampleData data;
  data = sample.getData();

You need an instance of SampleData first.

 SampleData getData()
  {
    return data;
  }

doesnt create its instance.

Maybe something like this can work: Call it as follow:

     SampleData data;
  data = sample.getData(w,h);

Then in SampleData class:

 SampleData getData(int width, int height)
  {
    data = new SampleData(width, height);
    return data;
  }
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
0

You need to create instance of sample using

sample=new Sample(width,height);
Nand
  • 614
  • 4
  • 10
0
    SampleData data;
      data = sample.getData();
   // in that place only you got error rite. bcoz you have to set values first in data then only you will get values..
    so You need an instance of SampleData first.

int w = bmInput.getWidth();
      int h = bmInput.getHeight();
      SampleData data;
      data = sample.getData(w,h);
    Then in SampleData class:

     SampleData getData(int width, int height)
      {
        data = new SampleData(width, height);
        return data;
      }
    now you can call 
    data = sample.getData(); this line it will get the value of getDate();

     SampleData getData()
      {
        return data;
      }
    doesnt create its instance.
Sethu
  • 430
  • 2
  • 13