Reputation: 41
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;
}
}
Upvotes: 1
Views: 94
Reputation: 428
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.
Upvotes: 0
Reputation: 22527
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;
}
Upvotes: 1
Reputation: 614
You need to create instance of sample using
sample=new Sample(width,height);
Upvotes: 0