我目前正在将Java程序转换为Android程序,并且一直出现此错误。我过去几天一直在尝试将其转换,但仍停留在这里
日志:
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)
第231行:data = sample.getData();
这是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);
}
}
}
这是Sample类的代码:
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;
}
}
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;
}
}
最佳答案
SampleData data;
data = sample.getData();
您首先需要一个SampleData实例。
SampleData getData()
{
return data;
}
不创建其实例。
也许这样的事情可以工作:
如下调用:
SampleData data;
data = sample.getData(w,h);
然后在SampleData类中:
SampleData getData(int width, int height)
{
data = new SampleData(width, height);
return data;
}
关于android - 继续在构造函数上收到NullPointerException错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22601828/