-1

It's my first question there so please don't be so cruel .

I've got problem with my program , it means i don't know why i have got NullpointerException , anybody knows why ?

My code : `package ex2;

public class Screen extends JPanel {

    private BufferedImage image ; 
    private Timer tm ; 
    private Graphics g ;
    private static int index = 0 ; 
    private ArrayList<String> paths = new Test().getArrayList();
    public Screen(){
        System.out.println(paths);
        String interval  ; 
        interval = JOptionPane.showInputDialog("Please write time interval "
                + "between images in miliseconds");
        tm = new Timer(Integer.parseInt(interval), new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    paint(index);
                    index += 1 ;
                    if (index >= paths.size())
                        System.exit(index);     
                } catch (IOException e1) {                  
                    e1.printStackTrace();
                }                                               
            }
        });
        tm.start();
    }
    public void paint(int i) throws IOException{
        image = ImageIO.read(new File(paths.get(i)));
        g.drawImage(image, 40, 30, image.getWidth() , image.getHeight() , null);
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
Karol Kot
  • 13
  • 3
  • 2
    The very least you could do would be pointing out which line causes the NPE. However you are not initializing `g` anywhere in this code, so there's your most likely cause – UnholySheep Apr 26 '17 at 18:33
  • 1
    Including your error and stack trace would go a long way towards helping you. – dckuehn Apr 26 '17 at 18:37
  • You should take a closer look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details about how painting works and how you can worlk with the system to achieve what you want – MadProgrammer Apr 26 '17 at 22:07

1 Answers1

1
private Graphics g ;

Looks null to me.

If you want to do custom painting then you need to override the paintComponent() method of your JPanel and use the Graphics object that is passed to the method.

Read the section from the Swing tutorial on Custom Painting for working examples that show the proper way to do painting.

Java slideshow without using ImageIcon

Why are you trying to do this without an ImageIcon? Why reinvent the wheel?

Just read the Image and create an ImageIcon and add the icon to a JLabel and the label to the frame. Then you change the image using the setIcon(...) method. The tutorial also has a section on How to Use Icons to get you started.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I'm sorry that I answer today , this is my task at school , I've made this aplication with ImageIcon but there is requirment to do this without ImageIcon , ok I've got not null and program show my image but just one . – Karol Kot Apr 27 '17 at 06:31