2
import java.awt.*;
import java.awt.event.*;

public class sample2 extends Frame
    {
    Button b[];
    public sample2()
        {
        super("trying");
        b=new Button[10];
        setLayout(new FlowLayout());
        for(int i=0;i<10;i++)
            add(b[i]);
        }
    public static void main(String args[])
        {
        sample2 obj=new sample2();
        obj.setSize(500,100);
        obj.setVisible(true);
        }
    }

Exception is as below

Exception in thread "main" java.lang.NullPointerException 
at java.awt.Container.addImpl(Container.java:1037) 
at java.awt.Container.add(Container.java:373) 
at sample2.<init>(sample2.java:13) 
at sample2.main(sample2.java:17) 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
vish4071
  • 5,135
  • 4
  • 35
  • 65
  • Hi vish4071, what is the error you're getting? – Alan Jun 26 '13 at 02:21
  • 3
    **What does the error say**? – SLaks Jun 26 '13 at 02:22
  • Let's not close this too soon. Let's give vish a chance to tell us about his error. – Hovercraft Full Of Eels Jun 26 '13 at 02:23
  • sorry, actually I am getting runtime error: – vish4071 Jun 26 '13 at 02:23
  • 1
    ie. Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1037) at java.awt.Container.add(Container.java:373) at sample2.(sample2.java:13) at sample2.main(sample2.java:17) – vish4071 Jun 26 '13 at 02:23
  • You are trying to add buttons which don't exist in your button array. `NUllPointerException`? – Java Devil Jun 26 '13 at 02:24
  • this should be closed as 'too localised' question. Why is this on hold now? – Jayan Jun 26 '13 at 02:35
  • @Jayan - Read the blog. "Too localized" no longer exists. Also read this: http://meta.stackexchange.com/questions/185102/responding-to-your-too-localized-concerns – Stephen C Jun 26 '13 at 02:40
  • I agree though. But the remedy would appear to be to close the Question as a duplicate of one of the many other questions where someone has an NPE due to not initializing an array element. – Stephen C Jun 26 '13 at 02:46
  • For example, this one: http://stackoverflow.com/questions/14677075/null-pointer-exception-for-array-of-objects – Stephen C Jun 26 '13 at 03:03
  • Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jun 26 '13 at 05:51

1 Answers1

5

Yes you create the Button array, but you never initialize the elements of the array before using. This means that you're adding null Buttons to your GUI. Create your Buttons first before using.

    for(int i=0;i<10;i++)
        b[i] = new Button();
        add(b[i]);
    }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    @vish4071: you're welcome. Please next time, post any and all complete error messages as well as indicate by code comment which lines of code cause the errors. Otherwise you force us to guess. – Hovercraft Full Of Eels Jun 26 '13 at 02:28
  • Also, please explain me the difference between Button and JButton class and similar distinctions. Some kind of link will help. – vish4071 Jun 26 '13 at 02:29
  • @vish4071: your code creates an GUI using AWT a very primitive GUI library created about 1990. JButton/JFrames, are part of the Swing GUI library. While it too is getting a bit dated, it's much more powerful and flexible than the AWT library. – Hovercraft Full Of Eels Jun 26 '13 at 02:31
  • 1
    Oh.. I get it. Can you please give me some link so that I can study that. I am still a beginner as you can see. – vish4071 Jun 26 '13 at 02:33
  • 1
    @vish4071: check out [Using Swing Components](http://docs.oracle.com/javase/tutorial/uiswing/components/index.html). – Hovercraft Full Of Eels Jun 26 '13 at 02:35
  • youtube, pdf etc anything will work. And sorry to bother you, if you have got some of this stuff, please help me with it. – vish4071 Jun 26 '13 at 02:36
  • I didn't know about that. Thankyou once again. – vish4071 Jun 26 '13 at 02:39