Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 继续获取构造函数上的NullPointerException错误_Android_Nullpointerexception - Fatal编程技术网

Android 继续获取构造函数上的NullPointerException错误

Android 继续获取构造函数上的NullPointerException错误,android,nullpointerexception,Android,Nullpointerexception,我目前正在将一个Java程序转换为一个Android程序,我一直有这个错误。在过去的几天里,我一直在尝试转换它,我被困在这里了 日志: 第231行:data=sample.getData() 以下是Entry2.downSample的代码片段: protected Sample sample; public void downSample() { int w = bmInput.getWidth(); int h = bmInput.getHeight(); int[] pixels

我目前正在将一个Java程序转换为一个Android程序,我一直有这个错误。在过去的几天里,我一直在尝试转换它,我被困在这里了

日志:

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

}
公共类样本数据
{
//降采样数据作为布尔网格。
受保护的布尔网格[];
保护字符字母;
/**
*构造器
* 
*@param-letter
*这是什么字母
*@参数宽度
*宽度
*@param高度
*高度
*/
公共样本数据(字符字母、整型宽度、整型高度)
{
网格=新布尔值[宽度][高度];
这个字母=字母;
}
/**
*设置采样数据的一个像素。
* 
*@param x
*x坐标
*@param y
*y坐标
*@param v
*要设置的值
*/
公共void setData(整数x,整数y,布尔值v)
{
网格[x][y]=v;
}
/**
*从样本中获取一个像素。
* 
*@param x
*x坐标
*@param y
*y坐标
*@返回请求的像素
*/
公共布尔getData(整数x,整数y)
{
返回网格[x][y];
}
公共空间清除()
{
对于(int x=0;xobj.getLetter())
返回1;
其他的
返回-1;
}
公共布尔等于(对象o)
{
返回(与(o)==0比较);
}
公共字符串toString()
{
返回“+”字母;
}
公共对象克隆()
{
SampleData obj=新的SampleData(字母,getWidth(),getHeight());
对于(int y=0;y
首先需要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;
  }

您需要使用创建示例的实例

sample=new Sample(width,height);

Entry2.java中的第231行是什么?如果这是关于缩小图像以在android中显示它,那么我想你会得到你的答案,请尝试在你的问题中添加更多内容,以便读者能够大致了解你的问题以及你想要什么……第231行是:data=sample.getData()@DreiDreiDrei,这是因为你还没有创建它的实例。我怎样才能创建SampleData的实例呢?我试图这样更改代码,但它使一些代码无效。我就我的问题插入了对代码的评论。
     SampleData data;
  data = sample.getData(w,h);
 SampleData getData(int width, int height)
  {
    data = new SampleData(width, height);
    return data;
  }
sample=new Sample(width,height);
    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.