-1

Error******************************************************************

Exception in thread "main" java.lang.NullPointerException
at java.awt.Window.init(Unknown Source)
at java.awt.Window.<init>(Unknown Source)
at java.awt.Frame.<init>(Unknown Source)
at java.awt.Frame.<init>(Unknown Source)
at javax.swing.JFrame.<init>(Unknown Source)
at Main.main(Main.java:12)

Code********************************************************************

import javax.swing.JFrame;
import javax.swing.JSlider;

import com.fazecast.jSerialComm.*;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame w = new JFrame();
        JSlider slider = new JSlider();
        slider.setMinimum(1023);
        w.add(slider);
        w.pack();
        w.setVisible(true);     
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Sarath V R
  • 23
  • 4
  • 2
    Are you sure that's the exact code you're running? Works fine on my machine. – Jonathan Sterling Mar 30 '17 at 10:01
  • The answers in that other question are very helpful in case of a `NullPointerException` in one’s own code. This exception comes from deep within AWT behind a call to a Swing constructor, so it’s different. @Japu_D_Cret – Ole V.V. Mar 30 '17 at 10:21
  • what is your Runtime Environment? exact Java Version and Manufacturer? (e.g. OpenJDK 1.8.0_121) – Japu_D_Cret Mar 30 '17 at 10:23
  • I downloaded and installed New JDK. Does eclipse can access this ? – Sarath V R Mar 30 '17 at 10:56

2 Answers2

1

Thank you Jonathan Sterling, Japu_D_Cret and chetan mehra. It worked well. I downloaded the exact version of Eclipse (Luna) prescribed in that video [ youtu.be/8B6j_yr9H8g ] and new JDK, Nothing else. The problem was with the IDE.

Sarath V R
  • 23
  • 4
0

you hadnt add setbounds(); in your java code try w.setbounds(100,100,200,200);.Usually null pointer exception came when the reference of an object is null.Try this .Hope it will work for you.

  • What is setbounds(); – Sarath V R Mar 30 '17 at 10:26
  • How can I add setbounds(); to my program ? – Sarath V R Mar 30 '17 at 10:27
  • 1
    it's a method of `java.awt.Component` from which your JFrame and your JSlider inherit - you can call it on your JFrame with `w.setBounds(int, int, int, int)` but this will most certainly not fix your problem since your error occurs in the initialization of the JFrame in the first line, see http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html for reference – Japu_D_Cret Mar 30 '17 at 10:29
  • Actually I found this code form YouTube , https://youtu.be/8B6j_yr9H8g , And not working on My computer – Sarath V R Mar 30 '17 at 10:35
  • which ide are you using? – chetan mehra Mar 30 '17 at 10:52
  • Since you are using a public class so name of the class and name of program must be same with.java extension.I run this code and it works fine in my machine – chetan mehra Mar 30 '17 at 11:03
  • I cannot believe this answer answers the question. I see no connection to the stacktrace reported. Also `w.pack()` should set a suitable window size. – Ole V.V. Mar 30 '17 at 11:26
  • IDE I am using : eclipse for android development. – Sarath V R Mar 30 '17 at 14:02