-1

Hello I need help drawing an image using the drawImage() method from this arraylist that holds images. It is supposed to be random. At first it was a program that floated down random letters, but after some suggestions I changed drawString() for the letters, to drawImage() for my arraylist of images. More specifically this line of code right here g.drawImage(PicList.get($ranNum), $ranNum, y, this); I do not expect anyone to do my homework for me. I just need some help figuring this out. NetBeans doesnt show any errors, but when I launch my program and the images are supposed to fall, I get a NullPointer exception. "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" Any help would be appreciated. I do not know much Java and things are starting to go over my head. Here are my full classes. EDIT: I understand why this error is happening, I know there are other questions like it. But my code is a little more complicated than those examples and they are not helping me right now.

/*******************************************************************************    **
* LearningLetterPanel.java
 * Panel class which which uses threads and overwrites the run method
* to display a panel which has letters that go from the top of the panel
 * to the bottom. The colors are set each iteration to a new color.
* It is used by Panel class
 *********************************************************************************/

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;


class LearningLetterPanel extends JPanel implements Runnable {
public static Thread letterThread = null;
private int y = 10;
static long nLetterDropped = 1;
RandNum rn = new RandNum();
int $ranNum = rn.ranNum();
String $letter = rn.ranNumLetter();
Color $letterColor= rn.ranNumColor();
    public java.util.List<Image> PicList; // here I am making a list to     store my images.

    public Image pic = null;

/***************************************************************************************
*  main method in the class for starting and stopping the thread
************************************************************************************/
LearningLetterPanel()  {
    if (letterThread == null) {
    letterThread = new Thread(this);
    letterThread.start();
        }
}

/***************************************************************************************
    *  Creates the thread and uses Thread.sleep to set the speed of the     movement
    ********************************************************************************    ****/

public void run() {
    Thread myThread = Thread.currentThread();
    while (letterThread == myThread) {
      try{
          Thread.sleep(20);
       }
       catch (InterruptedException e){}
       repaint();
        }
     }

/***************************************************************************************
* the paint method draws the letter based on color(ranNumLetter), location($ranNum and y)
* and speed (y += 3 with Thread.sleep from run())
*************************************************************************************/

    @Override
 public void paint(Graphics g) {

    g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 48));
    g.setColor(Color.white);
    //g.drawString($letter, $ranNum, y);
    g.drawImage(PicList.get($ranNum), $ranNum, y, this);
    y += 3;
    Dimension d = getSize();
    if (y > (d.width - 10))
       {y = 10;
       LearningLetterPanel.nLetterDropped +=1;
       $ranNum = rn.ranNum();
       this.$letter = rn.ranNumLetter();
       this.$letterColor = rn.ranNumColor();
       }

    g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 48));
    g.setColor($letterColor);
    //g.drawString($letter, $ranNum, y);

    g.drawImage(PicList.get($ranNum), $ranNum, y, this);
 }
        public void RandomImagePane() throws IOException {
        PicList = new ArrayList<Image>(25);

        // here I am adding the images to the list
        PicList.add(ImageIO.read(getClass().getResource("/images/aa.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/bb.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/cc.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/dd.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/ee.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/ff.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/gg.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/hh.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/ii.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/jj.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/kk.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/ll.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/mm.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/nn.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/oo.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/pp.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/qq.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/rr.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/ss.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/tt.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/uu.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/vv.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/ww.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/xx.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/yy.png")));
        PicList.add(ImageIO.read(getClass().getResource("/images/zz.png")));  



    }

/***************************************************************************************
*  sets thread to null which stops the thread
************************************************************************************/
 public static void stop() {
    letterThread = null;
    }

/***************************************************************************************
*  Returns the random letter when called
************************************************************************************/
public String getLetter() {
    return this.$letter;
}
  }

Here's a the class where these methods are from:

    import java.util.*;
    import java.awt.*;
    import java.util.List;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;

   /***************************************************************************************
       * This class is to be used for random numbers, letters, or colors
        * Using the Math.random class
   ************************************************************************************/

       public class RandNum {
        private String $alphabet="ABCDEFGHIJKLMNOPQRSTUVWXZY";
       private int $width=640;

       /*******************************************************************************       ********
      *     This class is to be used for random numbers
      * Using the Math.random class
     ************************************************************************************/
    public int ranNum() {
    int $ranNumber = 20 + (int)(Math.random() * $width);
    return $ranNumber;
}
/***             
        * This method is to be used for random letters
          * Using the Math.random class
        */
     public String ranNumLetter() {
    int $ranNumLetter = 0 + (int)(Math.random() * 26);
    String $letter = $alphabet.substring($ranNumLetter, $ranNumLetter+1);
    return $letter;
}
    /****
       * This method is to be used for colors
       * Using the Math.random class for each of the three RGB
      ************************************************************************************/

    public Color ranNumColor() {
    int $ranColorRed = 0 + (int)(Math.random() * 256);
    int $ranColorBlue = 0 + (int)(Math.random() * 256);
    int $ranColorGreen = 0 + (int)(Math.random() * 256);
    Color  c = new Color($ranColorRed, $ranColorBlue, $ranColorGreen);
   return c;
}
}
JavaJack
  • 3
  • 2
  • Hm, okay, I understand what the error is. I believe its saying that there's nothing in my list. The list is being added to after I do the g.drawImage() so that could be it, but where would I add it then? – JavaJack Dec 09 '15 at 20:54
  • No, its saying that there is *no list*. You only declared it, but there is no place where you initialize it (can't find any place where you ever call RandomImagePane()). That *probably* should be done in the constructor of LearningLetterPanel before starting the thread. – Durandal Dec 09 '15 at 21:19
  • So I have to instantiate the list, and THEN add things to it? – JavaJack Dec 09 '15 at 21:22
  • I looked around and people show an example of of instantiating an arraylist but I thought I already did that, can you show me how to call RandomImagePane()? – JavaJack Dec 09 '15 at 21:24
  • Because I thought that `PicList = new ArrayList(25);` was that – JavaJack Dec 09 '15 at 21:29
  • It is, but you never execute that line... – Durandal Dec 09 '15 at 21:35

1 Answers1

0

As it seems you don't initialize you list. Before you can refer to your PicList (that should be named picList btw, since its a field name and not a class name), you need to initialize it.

You have already written that in RandomImagePane(), but don't seem to call it (likewise that should be named initRandomImageList with lower case and the name describing its purpose).

(Sticking to your naming for now) Modify the construtor of LearningLetterPanel:

LearningLetterPanel()  {
    try {
        RandomImagePane();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    letterThread = new Thread(this);
    letterThread.start();
}

The alternative to catching the IOException would be to make the constructor throw IOException, but you would still need to catch it somewhere. I took the liberty of removing the test for letterThread == null, since that is always the case in the constructor (as long as you only create one instance)

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • Hey this fixed the problem thank you! I'm getting an index out of bounds error, now. But i'll try and look at that before I ask anymore questions. Thank you! – JavaJack Dec 09 '15 at 21:38