loadpixel()返回nullpointerexception

时间:2016-03-16 02:37:38

标签: java nullpointerexception processing

我正在使用netbeans中的处理来在一个ledstrip数组上播放电影,我正在使用OPC.class进行ledstrip fadecandy映射。此代码适用于处理草图,但是当我尝试在netbeans上使用它时,OPC.java的draw()方法中的loadpixel()会抛出一个nullpointer异常。

堆栈跟踪:

Exception in thread "Animation Thread" java.lang.NullPointerException
at processing.core.PApplet.loadPixels(PApplet.java:10625)
at com.processing.OPC.draw(OPC.java:139)
at com.processing.Video.draw(Video.java:62)
at processing.core.PApplet.handleDraw(PApplet.java:2402)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Video.java

public class Video extends PApplet
{

    OPC opc;
    Movie movie;

    public static void main(String args[])
    {

        PApplet.main(new String[] { "--present", "com.processing.Video" });

    }

    public void settings()
    {
        size(600, 240);     
    }

    public void setup()
    {  

    opc = new OPC(this, "192.168.15.10", 7890);

        for(int i=0; i<24; i++) {
            opc.ledStrip(i * 60, 60,
            300, i * 240 / 24 + 240 / 48, 240 / 24, PI, false);
        }

    movie = new Movie(this, "waffle.mp4");
    movie.loop();

    }

    public void movieEvent(Movie m)
    {
        m.read();
    }

    @Override
    public void draw()
    {

      if (movie.available() == true) {
            movie.read(); 
            }
        image(movie, 0, 0, width, height);

    }


}

OPC.java

public class OPC extends PApplet implements Runnable  
{

  Thread thread;
  Socket socket;
  OutputStream output, pending;
  String host;
  int port;
  int height = 240;
  int width = 600;
  int[] pixelLocations;
  byte[] packetData;
  byte firmwareConfig;
  String colorCorrection;
  boolean enableShowLocations;
  PApplet parent;

  OPC(PApplet parent, String host, int port)
  {
    this.host = host;
    this.port = port;
    thread = new Thread(this);
    thread.start();
    this.enableShowLocations = true;
    registerMethod("draw", this);
  }

  public void led(int index, int x, int y){  

    if (pixelLocations == null) {
      pixelLocations = new int[index + 1];
    } else if (index >= pixelLocations.length) {
      pixelLocations = Arrays.copyOf(pixelLocations, index + 1);
    }
    pixelLocations[index] = x + 600 * y;
  }

  public void ledStrip(int index, int count, float x, float y, float spacing, float angle, boolean reversed)
  {

    float s = sin(angle);
    float c = cos(angle);
    for (int i = 0; i < count; i++) {
      led(reversed ? (index + count - 1 - i) : (index + i),
        (int)(x + (i - (count-1)/2.0) * spacing * c + 0.5),
        (int)(y + (i - (count-1)/2.0) * spacing * s + 0.5));

    }

  }


  void showLocations(boolean enabled)
  {
    enableShowLocations = enabled;
  }

  void setColorCorrection(String s)
  {
    colorCorrection = s;
    sendColorCorrectionPacket();
  }

  void sendFirmwareConfigPacket()
  {
    if (pending == null) {
      return;
    }

    byte[] packet = new byte[9];
    packet[0] = (byte)0x00; // Channel (reserved)
    packet[1] = (byte)0xFF; // Command (System Exclusive)
    packet[2] = (byte)0x00; // Length high byte
    packet[3] = (byte)0x05; // Length low byte
    packet[4] = (byte)0x00; // System ID high byte
    packet[5] = (byte)0x01; // System ID low byte
    packet[6] = (byte)0x00; // Command ID high byte
    packet[7] = (byte)0x02; // Command ID low byte
    packet[8] = (byte)firmwareConfig;

    try {
      pending.write(packet);
    } catch (Exception e) {
      dispose();
    }
  }

  void sendColorCorrectionPacket()
  {
    if (colorCorrection == null) {
      return;
    }
    if (pending == null) {
      return;
    }

    byte[] content = colorCorrection.getBytes();
    int packetLen = content.length + 4;
    byte[] header = new byte[8];
    header[0] = (byte)0x00;               // Channel (reserved)
    header[1] = (byte)0xFF;               // Command (System Exclusive)
    header[2] = (byte)(packetLen >> 8);   // Length high byte
    header[3] = (byte)(packetLen & 0xFF); // Length low byte
    header[4] = (byte)0x00;               // System ID high byte
    header[5] = (byte)0x01;               // System ID low byte
    header[6] = (byte)0x00;               // Command ID high byte
    header[7] = (byte)0x01;               // Command ID low byte

    try {
      pending.write(header);
      pending.write(content);
    } catch (Exception e) {
      dispose();
    }
  }

  public void draw()
  {
    if (pixelLocations == null) {
      return;
    }
    if (output == null) {
      return;
    }

    int numPixels = pixelLocations.length;
    int ledAddress = 4;

    setPixelCount(numPixels);
    println("pixel loading");
    loadPixels();
    println("pixel loaded123");
    for (int i = 0; i < numPixels; i++) {
      int pixelLocation = pixelLocations[i];
      int pixel = pixels[pixelLocation];

      packetData[ledAddress] = (byte)(pixel >> 16);
      packetData[ledAddress + 1] = (byte)(pixel >> 8);
      packetData[ledAddress + 2] = (byte)pixel;
      ledAddress += 3;

      if (true) {
        pixels[pixelLocation] = 0xFFFFFF ^ pixel;
      }
    }

    writePixels();

    if (enableShowLocations) {
      updatePixels();
      print("a");
    }
  }

  void setPixelCount(int numPixels)
  {
    int numBytes = 3 * numPixels;
    int packetLen = 4 + numBytes;
    if (packetData == null || packetData.length != packetLen) {
      // Set up our packet buffer
      packetData = new byte[packetLen];
      packetData[0] = (byte)0x00;              
      packetData[1] = (byte)0x00;              
      packetData[2] = (byte)(numBytes >> 8);   
      packetData[3] = (byte)(numBytes & 0xFF); 
    }
  }

  void setPixel(int number, int c)
  {
      println("set");
    int offset = 4 + number * 3;
    if (packetData == null || packetData.length < offset + 3) {
      setPixelCount(number + 1);
    }

    packetData[offset] = (byte) (c >> 16);
    packetData[offset + 1] = (byte) (c >> 8);
    packetData[offset + 2] = (byte) c;
  }

  int getPixel(int number)
  {
      println("get");
    int offset = 4 + number * 3;
    if (packetData == null || packetData.length < offset + 3) {
      return 0;
    }
    return (packetData[offset] << 16) | (packetData[offset + 1] << 8) | packetData[offset + 2];
  }

  void writePixels()
  {
    println("write");
    if (packetData == null || packetData.length == 0) {
      return;
    }
    if (output == null) {
      return;
    }

    try {
      output.write(packetData);
    } catch (Exception e) {
      dispose();
    }
  }

  public void dispose()
  {

    if (output != null) {
      println("Disconnected from OPC server");
    }
    socket = null;
    output = pending = null;
  }

  public void run()
  {

      println("?");
      if(output == null) { // No OPC connection?
        try {              // Make one!
          socket = new Socket(host, port);
          socket.setTcpNoDelay(true);
          pending = socket.getOutputStream(); 
          println("Connected to OPC server");
          sendColorCorrectionPacket();        
          sendFirmwareConfigPacket();         
          output = pending;                   
        } catch (ConnectException e) {
          dispose();
        } catch (IOException e) {
          dispose();
        }
      }

      try {
        Thread.sleep(500);
      }
      catch(InterruptedException e) {
      }

  }
}

1 个答案:

答案 0 :(得分:0)

您应该只有一个扩展PApplet的类。

将该课程视为您的&#34;主要&#34;类。需要使用Processing函数的任何其他类都需要该主类的实例才能访问Processing函数。您可以使用this关键字传递它。

事实上,您已经这样做了 - 请注意您将this传递到OPC类,然后将其存储在parent参数中。你永远不会对该参数做任何事情。

所以第1步是要从extends PApplet课程中移除OBC

public class OPC implements Runnable  

这会导致一些编译错误。没关系,我们稍后会修复它们。

第2步是将PApplet parent参数实际存储在类级变量中。

OPC(PApplet parent, String host, int port){
   this.parent = parent;
   //rest of constructor

现在你有了,第3步是通过使用parent变量来访问处理函数来修复任何编译错误。

parent.println("pixel loading");
parent.loadPixels();

可以在Processing in eclipse tutorial中找到更多信息。它适用于日食,但同样的原则也适用于netbeans。