gpt4 book ai didi

android - 继续在构造函数上出现 NullPointerException 错误

转载 作者:行者123 更新时间:2023-11-30 03:00:39 26 4
gpt4 key购买 nike

我目前正在将 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 的代码 fragment :

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);
}
}
}

下面是示例类的代码:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com