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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:14:36+00:00 2026-05-16T07:14:36+00:00

The Below code does not fail to compile but however at runtime it says

  • 0

The Below code does not fail to compile but however at runtime it says java.lang.NullPointerException at Line at line number 20 and 41. Also i am little bit curious to know what is Null Pointer Exception, what happens at runtime ?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Tool 
    {
     private JToolBar toolbar1;
     private JToolBar toolbar2;
     private JPanel panel;
     public Tool()
        {
         JFrame frame= new JFrame();
         panel = new JPanel();
         panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
         JButton one = new JButton("one");
         JButton two = new JButton("two");
         JButton three = new JButton("three");
         JButton four = new JButton("four");
             toolbar1 = new JToolBar();
             toolbar2 = new JToolBar();
         toolbar1.add(one);
         toolbar1.add(two);
         toolbar2.add(three);
         toolbar2.add(four);
         toolbar1.setAlignmentX(0);
         toolbar2.setAlignmentX(0);
         panel.add(toolbar1);
         panel.add(toolbar2);
         frame.add(panel,BorderLayout.NORTH);

         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
         frame.setSize(400,300);
         frame.setTitle("ZOOP");
         frame.setVisible(true);
        }




        public static void main (String args[])
        {
         Tool zoop = new Tool();
        }


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:14:36+00:00Added an answer on May 16, 2026 at 7:14 am

    You are passing a null on the following methods….

         panel.add(toolbar1);
         panel.add(toolbar2);
    

    It’s because the following haven’t been initialized.

     private JToolBar toolbar1;
     private JToolBar toolbar2;
    

    Definition of NullPointerException

    Thrown when an application attempts to
    use null in a case where an object is
    required. These include:

    • Calling the instance method of a null object.
    • Accessing or modifying the field of a null object.
    • Taking the length of null as if it were an array.
    • Accessing or modifying the slots of null as if it were an array.
    • Throwing null as if it were a Throwable value.

    Initialize it

     JToolBar toolbar1 = new JToolBar(SwingConstants.HORIZONTAL);
     JToolBar toolbar2 = new JToolBar(SwingConstants.VERTICAL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below does not work but meant to illustrate what I want to
Consider the code below. I don't understand why my GCC compiler does not try
Below is some sample code. The window does not resize (python3.2, on a Mac,
Possible Duplicate: C++'s “placement new” in the below code what does Line 3 represents,
Why does the below code give Seg. Fault at last line? char* m=ReadName(); printf(\nRead
I have to code below - works great in IE and Opera, but does
Or put another way, why does semicolon insertion fail, leaving the code below broken.
I work on VS 2008 with C#. This below code does not work for
Unfortunately this code below does not print anything on the firebug console, neither does
The below code does not fully disable the sortables on the start event. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.