0

What's wrong with this code? It works in NetBeans. I've tried to open it in browser, but there is Null Pointer Exception error, probably because of image (without graphic it works).

public class Sth extends JApplet {

private Image img;

@Override
public void init() {
    img = getImage(getCodeBase(), "logo.png");

    setSize(200,200);
    JPanel panel = new JPanel();
    panel.setLayout(null);
    add(panel);

    JLabel cos = new JLabel();
    cos.setIcon(new ImageIcon(img));
    cos.setBounds(20,20,100,100);
    panel.add(cos);
}}
user1950221
  • 13
  • 1
  • 7
  • Is the image in the same location as the class file? – Reimeus Nov 07 '13 at 13:12
  • check your pass to image. NullPointerEx happens, when obj = null. it cant find yout image. – Andrew Evt Nov 07 '13 at 13:12
  • Does your browser have Java/installed/enabled?? – Paul Samsotha Nov 07 '13 at 13:22
  • Check out applet deployment tutorial http://docs.oracle.com/javase/tutorial/deployment/applet/deployingApplet.html – Paul Samsotha Nov 07 '13 at 13:25
  • 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 2) `setSize(200,200);` Don't do that in an applet. The size is set in HTML. – Andrew Thompson Nov 07 '13 at 23:24

0 Answers0