0

i'm completely new to any sort or applet programming and my code is really rough. Currently i am making an applet that allows you to learn about different countries based on what continents they are in. It's really simple stuff but i am still struggling. Currently i am receiving the error

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException

at Culminating1.instructions1(Culminating1.java:69). This line is a simple g.setColor(Color.yellow);.

My codes really rough and needs a lot of work so any kind of help is appreciated.

Here is the rest of my code. Thanks a lot!

import java.applet.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JComponent;
import java.awt.Color.*;

public class Culminating1 extends Applet {

    private Image world, destination, globetrot, rice;

    private Button africa, americas, asia, europe, instructions, japan, china, qatar, uae,   thailand;
 private Graphics g = null;

    public void init() {

        this.setLayout(null);
        asia = new Button("Asia");
        africa = new Button("Africa");
        europe = new Button("Europe");
        americas = new Button("Americas");
        instructions = new Button("Instructions");
        add(asia);
        asia.setBounds(10, 120, 65, 25);
        add(africa);
        africa.setBounds(10, 190, 65, 25);
        add(americas);
        americas.setBounds(10, 260, 65, 25);
        europe.setBounds(10, 330, 65, 25);
        add(europe);
        instructions.setBounds(10, 410, 65, 25);

        add(instructions);
        world = getImage(getCodeBase(), "world.gif");
        destination = getImage(getCodeBase(), "Destination2.png");
        globetrot = getImage(getCodeBase(), "GlobeTrot.png");

    } // init method

     @Override
     public void paint(Graphics g)  
    {

        g.setColor(Color.orange);
        g.fillRect(0, 0, 1000, 1000);
        g.drawImage(world, 200, 100, 300, 300, this);
        g.drawImage(destination, 150, 425, 450, 80, this);
        g.drawImage(globetrot, 150, 10, 450, 80, this);

    } // paint method

    @Override
    public boolean action(Event e, Object o) {
        if (e.target == instructions) {
            instructions1();
        } else if (e.target == asia) {
            asia();
        }
        return false;
    }

     public void instructions1() {
        JFrame jp1 = new JFrame();
        Culminating1 a=new Culminating1 ();
        jp1.getContentPane().add(a, BorderLayout.CENTER);
        jp1.setSize(new Dimension(500,500));
        jp1.setVisible(true);

        g.setColor(Color.yellow);
        g.fillRect(0, 0, 1000, 1000);
        Font f;
        f = new Font("Broadway", Font.PLAIN, 20);
        g.setFont(f);
        g.setColor (Color.black);
        g.drawString("Welcome to Globe Trot!", 200, 20);
        g.drawString("In this game you will learn about countries around the world.", 200, 40);
        g.drawString("First choose an area of the world,", 200, 80);
        g.drawString("then at random a well known landscape will appear.", 200, 120);
        g.drawString("Your goal is to guess the country where the landscape is from.", 200, 160);
        g.drawString("If you are unable to guess after 10 seconds a flag will appear", 200, 200);
        g.drawString("from the same country. You have one minute to guess the country", 200, 240);
        g.drawString("before you move onto the next country. There are five in total", 200, 280);
    }

} // Test class

mKorbel
  • 108,320
  • 17
  • 126
  • 296
user3204820
  • 229
  • 1
  • 4
  • 16
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jan 18 '14 at 00:34
  • 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). – Andrew Thompson Jan 18 '14 at 00:34

1 Answers1

2

You state:

This line is a simple g.setColor(Color.yellow);.

Nothing simple about it. You set g to null on this line:

private Graphics g = null;

and then call a method on it, and so it should not come as a surprise that you're getting a NPE.

Solution:

  • Don't call methods on variables set to null.
  • Specifically for the Graphics object, don't use it outside of the painting methods except when Graphics objects extracted from images such as with BufferedImages.
  • The Graphics parameter of the paint method is a parameter variable, and thus has a scope limited to the method only. It has no effect on the g field that you're using, and in fact shadows the field.

Also:

  • You're better off creating Swing GUI's rather than AWT GUI's, unless you absolutely must create AWT Applets for a class.
  • You seem to be trying to create a Swing JFrame inside of an AWT Applet which really doesn't make sense. What is the motivation behind this very unusual bit of code?
  • If your intent is to create a Swing GUI, then don't have your class extend Applet, and don't have a paint method.
  • If Swing, then you should have a class that extends JPanel and override its paintComponent(Graphics g) method and do all painting there.
  • It's usually easier to show text in a JLabel or in a JTextComponent such as a JTextArea.

Edit
You state in comment

Thanks for the advice i literally have no idea what i am doing but i want to use the graphics class outside of the paint method is there anyway i am able to do this or is it impossible.

It's possible but requires tricks.

  • You could get the Graphics object from a BufferedImage and draw on it,
  • but you're still then going to need to draw that BufferedImage in your paint (or again, better, a paintComponent(Graphics g) method of a JPanel.
  • Again, if you're just posting text, you're better off not using a Graphics object at all, but instead using a component such as a JLabel, JTextField, JTextArea or something similar.
  • A good simple tutorial on use of Swing Graphics can be found here: Lesson: Performing Custom Painting.
  • A more in depth article can be found here: Painting in AWT and Swing

Edit 2
Next comment:

So what i have is a basic intro screen and what i want to do is when the user clicks one of the buttons it takes them to another screen. I've been trying to use a method to do this.

I think that it would be better to either swap text in a text component, or swap JPanels using a CardLayout.

On the screen which they are being sent to after clicking the button there are graphics needed this is why i am trying to bring in the graphics class.

Again you could use a CardLayout, swap JPanels from the introduction JPanel to a display JPanel, and do your graphics in the display JPanel's paintComponent(Graphics g) method override.

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
  • Don't mind the -1 vote, but if you're going to do that, at least post a comment or a better answer. If it is in fact better, I'll even up-vote ya. – Hovercraft Full Of Eels Jan 17 '14 at 01:39
  • Well, it's not exactly fair for skewering the OP for using Swing inside an applet. Swing components descend from AWT. If you're using an applet, as you note, by default you're using a Panel but OP says he's new to Java. You may want to use Swing in an applet to have a standard LnF across platforms. It's not crazy to use Swing like this. Edit: -1 vote was a mistake. Was supposed to be upvote. Sorry! – mttdbrd Jan 17 '14 at 01:39
  • Thanks for the advice i literally have no idea what i am doing but i want to use the graphics class outside of the paint method is there anyway i am able to do this or is it impossible – user3204820 Jan 17 '14 at 01:42
  • 1
    @mttdbrd: I wasn't skewering, but rather noting that it is odd code, and asked for justificatino. But yeah, regardless of your comment, it kind of is bad to do. 1) If you're going to code a Swing applet, do a JApplet, not an Applet, and don't create a JFrame, which is for non-applet stand alone GUI's, in it. 2) If you do need an additional window in association with a JApplet, it should be a JDialog. 3) the code above is misleading. Since it is in fact creating a JFrame, the paint method does nothing. – Hovercraft Full Of Eels Jan 17 '14 at 01:42
  • @user3204820: Please see edit to answer. – Hovercraft Full Of Eels Jan 17 '14 at 01:46
  • Is there anyway you could show me an example of what you mean to do with the JDialog. Once again i'm sorry still learning how to use the applet and it really is helping me out a lot – user3204820 Jan 17 '14 at 01:46
  • @HovercraftFullOfEels Agreed. – mttdbrd Jan 17 '14 at 01:47
  • @user3204820: first what are you trying to do? Create a stand alone GUI? If so, extend JPanel, override paintComponent and display it in a JFrame. Don't have any applet or JApplet code if you don't need it. – Hovercraft Full Of Eels Jan 17 '14 at 01:48
  • So what i have is a basic intro screen and what i want to do is when the user clicks one of the buttons it takes them to another screen. I've been trying to use a method to do this. On the screen which they are being sent to after clicking the button there are graphics needed this is why i am trying to bring in the graphics class. – user3204820 Jan 17 '14 at 01:57
  • @HovercraftFullOfEels – user3204820 Jan 17 '14 at 02:03
  • @user3204820: Please see edit 2 to answer. – Hovercraft Full Of Eels Jan 17 '14 at 02:05
  • now when you say i should method override paintComponent(Graphics g) what does that mean? @HovercraftFullOfEels – user3204820 Jan 17 '14 at 02:11
  • @user3204820: Please check the first link in my first edit to answer: [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) – Hovercraft Full Of Eels Jan 17 '14 at 02:13
  • 1
    *"want to use the graphics class outside of the paint method"* Paint to a `BufferedImage`. Display it in a `JLabel`. On change, call `label.repaint()`.. – Andrew Thompson Jan 18 '14 at 00:37