Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/The complete guide to exception in thread "awt-eventqueue-0" java.lang.nullpointerexception
Next
Answered
Nathan Voisin
  • 19
Nathan Voisin
Asked: May 11, 20222022-05-11T18:56:44+00:00 2022-05-11T18:56:44+00:00In: Programs

The complete guide to exception in thread “awt-eventqueue-0” java.lang.nullpointerexception

  • 19

. Advertisement .

..3..

. Advertisement .

..4..

I get the error: exception in thread “awt-eventqueue-0” java.lang.nullpointerexception when I try to run the program below:

package components;
 
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 
 public class Workshop extends JPanel
  implements ItemListener {
 JCheckBox winterhatButton;
 JCheckBox sportshatButton;
 JCheckBox santahatButton;
 JCheckBox redshirtButton;
 JCheckBox brownshirtButton;
 JCheckBox suitButton;
 JCheckBox denimjeansButton;
 JCheckBox blackpantsButton;
 JCheckBox khakipantsButton;
 
 
  StringBuffer choices;
 JLabel pictureLabel;
 
 public Workshop() {
  super(new BorderLayout());
 
  //Create the check boxes.
  winterhatButton = new JCheckBox("Winter Hat");
  winterhatButton.setMnemonic(KeyEvent.VK_Q);
 
 
  sportshatButton = new JCheckBox("Sports Hat");
  sportshatButton.setMnemonic(KeyEvent.VK_W);
 
 
  santahatButton = new JCheckBox("Santa hat");
  santahatButton.setMnemonic(KeyEvent.VK_E);
 
 
  redshirtButton = new JCheckBox("Red Shirt");
  redshirtButton.setMnemonic(KeyEvent.VK_R);
 
 
  brownshirtButton = new JCheckBox("Brown Shirt");
  brownshirtButton.setMnemonic(KeyEvent.VK_T);
 
 
  suitButton = new JCheckBox("Suit");
  suitButton.setMnemonic(KeyEvent.VK_Y);
 
 
  suitButton = new JCheckBox("Denim Jeans");
  suitButton.setMnemonic(KeyEvent.VK_U);
 
 
  blackpantsButton = new JCheckBox("Black Pants");
  blackpantsButton.setMnemonic(KeyEvent.VK_I);
 
 
  khakipantsButton = new JCheckBox("Khaki Pants");
  khakipantsButton.setMnemonic(KeyEvent.VK_O);
 
 
 
  //Register a listener for the check boxes.
 
  winterhatButton.addItemListener(this);
  sportshatButton.addItemListener(this);
  santahatButton.addItemListener(this);
  redshirtButton.addItemListener(this);
  brownshirtButton.addItemListener(this);
  suitButton.addItemListener(this);
  denimjeansButton.addItemListener(this);
  blackpantsButton.addItemListener(this);
  khakipantsButton.addItemListener(this);
 
 
  //Indicates
  choices = new StringBuffer("---------");
 
 
  //Set up the picture label
  pictureLabel = new JLabel();
  pictureLabel.setFont(pictureLabel.getFont().deriveFont(Font.ITALIC));
  updatePicture();
 
  //Put the check boxes in a column in a panel
  JPanel checkPanel = new JPanel(new GridLayout(0, 1));
  checkPanel.add(winterhatButton);
  checkPanel.add(sportshatButton);
  checkPanel.add(santahatButton);
  checkPanel.add(redshirtButton);
  checkPanel.add(brownshirtButton);
  checkPanel.add(suitButton);
  checkPanel.add(denimjeansButton);
  checkPanel.add(blackpantsButton);
  checkPanel.add(khakipantsButton);
 
 
  add(checkPanel, BorderLayout.LINE_START);
  add(pictureLabel, BorderLayout.CENTER);
  setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
 }
 
 
  /** Listens to the check boxes. */
 public void itemStateChanged(ItemEvent e) {
  int index = 0;
  char c = '-';
  Object source = e.getItemSelectable();
 
  if (source == winterhatButton) {
  index = 0;
  c = 'q';
  } else if (source == sportshatButton) {
  index = 1;
  c = 'w';
  } else if (source == santahatButton) {
  index = 2;
  c = 'e';
  } else if (source == redshirtButton) {
  index = 3;
  c = 'r';
  } else if (source == brownshirtButton) {
  index = 4;
  c = 't';
  } else if (source == suitButton) {
  index = 5;
  c = 'y';
  } else if (source == denimjeansButton) {
  index = 6;
  c = 'u';
  } else if (source == blackpantsButton) {
  index = 7;
  c = 'i';
  } else if (source == khakipantsButton) {
  index = 8;
  c = 'o';
  } 
 
 
  if (e.getStateChange() == ItemEvent.DESELECTED) {
  c = '-';
  }
 
  //Apply the change to the string.
  choices.setCharAt(index, c);
 
  updatePicture();
 }
 
 
 protected void updatePicture() {
  //Get the icon corresponding to the image.
  ImageIcon icon = createImageIcon(
  "images/bear/bear-"
  + choices.toString()
  + ".gif");
  pictureLabel.setIcon(icon);
  pictureLabel.setToolTipText(choices.toString());
  if (icon == null) {
  pictureLabel.setText("Missing Image");
  } else {
  pictureLabel.setText(null);
  }
 }
 
 /** Returns an ImageIcon, or null if the path was invalid. */
 protected static ImageIcon createImageIcon(String path) {
  java.net.URL imgURL = Workshop.class.getResource(path);
  if (imgURL != null) {
  return new ImageIcon(imgURL);
  } else {
  System.err.println("Couldn't find file: " + path);
  return null;
  }
 }
 
  private static void createAndShowGUI() {
  //Create and set up the window.
  JFrame frame = new JFrame("Build a Bear at Safeer's Workshop!");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
  //Create and set up the content pane.
  JComponent newContentPane = new Workshop();
  newContentPane.setOpaque(true); //content panes must be opaque
  frame.setContentPane(newContentPane);
 
  //Display the window.
  frame.pack();
  frame.setVisible(true);
 }
 
 public static void main(String[] args) {
  //Schedule a job for the event-dispatching thread:
  //creating and showing this application's GUI.
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
  public void run() {
  createAndShowGUI();
  }
  });
 }
 }

The error appears the system notifies as follows:

> run components.Workshop
 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
 at components.Workshop.<init>(Workshop.java:75)
 at components.Workshop.createAndShowGUI(Workshop.java:195)
 at components.Workshop.access$0(Workshop.java:189)
 at components.Workshop$1.run(Workshop.java:209)
 at java.awt.event.InvocationEvent.dispatch(Unknown Source)
 at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
 at java.awt.EventQueue.access$000(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source) 
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)

I tried to solve it with another sample. I got the reference in the community forum, but it still returned an invalid result. If someone knows the solution, please give me the support. Thanks!

runtime-error
  • 3 3 Answers
  • 120 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-05-29T02:52:06+00:00Added an answer on May 29, 2022 at 2:52 am

    The cause: The Public Workshop() is near the top of code.

    suitButton = new JCheckBox("Suit"); 
    suitButton.setMnemonic(KeyEvent.VK_Y); 
    
    
    suitButton = new JCheckBox("Denim Jeans"); 
    suitButton.setMnemonic(KeyEvent.VK_U);

    The solution: 

    suitButton = new JCheckBox("Suit"); 
    suitButton.setMnemonic(KeyEvent.VK_Y); 
    
    denimjeansButton = new JCheckBox("Denim Jeans"); 
    denimjeansButton.setMnemonic(KeyEvent.VK_U);
    • 14
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Léa Faure
    2022-05-11T18:56:47+00:00Added an answer on May 11, 2022 at 6:56 pm

    NullPointerExceptions are among the easier exceptions to diagnose, frequently. If you receive an exception in Java, you will see the stack trace. (That’s what your second quote block is called by the way.) You read the stack trace from top to bottom. You will often see exceptions in Java library code and native implementations methods. For diagnosis, you can skip those files until you see the code you wrote.

    Next, look at the line indicated. Look at each object (instantiated classes) on that line. One of them wasn’t created and you tried using it. To check if the constructor was called on this object, you can look up your code. If you did not, that is your problem. You need to call new Classname( arguments) to instantiate the object. Another frequent cause of NullPointerExceptions is accidentally declaring an object with local scope when there is an instance variable with the same name.

    Your constructor for Workshop at line 75 was the culprit in your exception. init> is the constructor of a class. You’ll notice the line if you look at that line in your code.

    denimjeansButton.addItemListener(this);

    This line contains two objects: denimjeansButton, and this. this refers to the class instance that you are currently using. If you’re in a constructor, it cannot be this. denimjeansButton are your culprits. That object was never instantiated. You can either remove the denimjeansButton reference or instantiate it.

    • 10
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. oswald83
    2022-05-23T07:00:37+00:00Added an answer on May 23, 2022 at 7:00 am
    int randomNumber = rn.nextInt(words.length);

    Above is shown in line 24

    This would mean the the word could be not null. Which means that readArray("words.txt") didn’t return anything.
    Looking at the code for readArray, that would suggest you’re getting a FileNotFoundException, which means the file doesn’t exist. Using the debugger to figure out the problem.

    • 8
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.