add(Component) method question

Posted on 2001-06-06
Last Modified: 2010-03-31

In the init() method, I use add(myLabel). If myLabel is null, we get a nullPointer exception. Where is the method that throws that exception? I could not find it in the Component class where the add methods are written.
Question by:HanRui
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
LVL 92

Expert Comment

ID: 6161950
It's in the Applet class or one of it descendants, probably Container.
Why do you need to know?

What you are doing is invalid and that is why you are getting an exception.

Author Comment

ID: 6161967
I looked in the classes all the way up the hierarchy, and there is no add method that is defined with "throws exception." I want to understand this so that I can communicate it to students. If an exception is reported, there must be a method somewhere that throws it.
LVL 92

Expert Comment

ID: 6161999
Hang on I'll find it for you.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 92

Expert Comment

ID: 6162006
Sorry, it's too early in the morning.
The exception won't be throw explicitly, it'll be thrown when you try and do something like this:

Object a = null;
String s = a.toString()  // This will cause a NPE to be thrown

I'll find the spot in the code where its occurring for you.
LVL 92

Accepted Solution

objects earned 100 total points
ID: 6162009
From the NPE javadoc:

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.
LVL 92

Expert Comment

ID: 6162014
It's thrown from the addImpl method in the Container class.

Another easy way to find where an exception originated is to use the printStackTrace() method of the Exception class.


catch (Exception e)

Author Comment

ID: 6162019
Ok. I still want to see a method somewhere with throws exception in it. The priniciples that you listed make sense, but the more general principle is that for an exception to be thrown at all, there must be some method somewhere with "throws exception" as part of its definition.

  at java.awt.Container.addImpl(Compiled Code)
  at java.awt.Container.add(Compiled Code)
  at Test1.init(Compiled Code)
* at netscape.applet.DerivedAppletFrame$InitAppletEvent.dispatch(Compiled Code)
  at java.awt.EventDispatchThread$EventPump.dispatchEvents(Compiled Code)
  at Code)
  at netscape.applet.DerivedAppletFrame$ Code)

The above is the message output in the Java console. The addImpl method does not throw an exception. I did see an IllegalArgumentException in the Container class, but no method with throws exception.

Expert Comment

by:Igor Bazarny
ID: 6162031

There is no need to declare 'throws Exception' to throw NulPointerException. Exception derived from RuntimeException end Error can be thrown from anywhere, no matter what throws clause says.
add() is method family of Container (not Component) and real implementation is in addImpl(). It's no wonder that it throws NullPointerException when you add null component. In fact, it prevents you from more serious problems--ability to add null component would possibly cause NullPointerException far from call to add().

You would better find a Label to add. And watch out, from my experience, NullPointerException is most often exception, and most helpful--you can tell from stacktrace where you try to use uninitialized reference. Much better than simply fall with 'General Protection Fault' message.

Igor Bazarny
Brainbench MVP for Java 1

Author Comment

ID: 6162049
Thank you. I already closed the question, but I appreciate your feedback.
LVL 92

Expert Comment

ID: 6162054
The exception is thrown by the JVM as a result of calling a method on a null object reference.

Thanks for the points :)

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jsp insert to database example 2 60
Java Restore security prompts not working 10 125
dao vs facade design patterns 2 33
Netbeans and org.apache.commons.lang3 issue 3 24
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question