Exception in "AWT-EventQueue-0" java.lang.NullPointerException - java

This programs take in a string for a parameter. Currently filled with:
"http://localhost/media/svu.mp4"
I have made sure the URL exists.
I am using the VLCj library to create the mediaPlayerComponent (which is placed inside the container (JPanel mainPanel) ). The component mainPanel is then placed inside the JLayeredPanel layers. On top of that, I place a clear (non-opaque) layer (JPanel glassPane). According to everything I've read, this should be working and Eclipse isn't showing any errors or warnings.
The stack trace is as follows:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at client.test.Client.<init>(Client.java:62)
at client.test.Client$1.run(Client.java:44)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
....
The code is below. Line 62 is marked with a comment. The JPanels and JLayeredPanel, as well as the windowDimensions are all created as static objects above the main method in my code.
Any and all help is greatly appreciated.
static JLayeredPane layers = new JLayeredPane();
static JPanel mainPanel, glassPane = new JPanel();
public Client(String toPlay) {
JFrame frame = new JFrame("Client");
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
MediaPlayer mediaPlayer= mediaPlayerComponent.getMediaPlayer();
frame.setSize(windowDimensions[0], windowDimensions[1]);
frame.setLayout(new BorderLayout());
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(layers, BorderLayout.CENTER);
layers.setBounds(0,0,windowDimensions[0], windowDimensions[1]);
mainPanel.setBackground(Color.black); /* This is line 62 */
mainPanel.setBounds(0,0,windowDimensions[0], windowDimensions[1]);
mainPanel.setOpaque(true);
mainPanel.add(mediaPlayerComponent);
glassPane.setBackground(Color.white);
glassPane.setBounds(0,0,windowDimensions[0], windowDimensions[1]);
glassPane.setOpaque(false);
layers.add(mainPanel, new Integer(0), 0);
layers.add(glassPane, new Integer(1), 0);
frame.setVisible(true);
mediaPlayer.playMedia(toPlay);
}

mainPanel has not been intitialized, even if it looks like it has. You have this code:
static JPanel mainPanel, glassPane = new JPanel();
This only inititalzes glassPane. In order to initialize mainPanel you have to change your code to this:
static JPanel mainPanel = new JPanel(), glassPane = new JPanel();

You haven't initialized mainPanel. Try adding mainPanel = new JPanel(); above the error line.
You'll also need to call frame.add(mainPanel); after you initialize the panel.

Related

How to fix GUI components "leaving" their panels or taking up more space they they are supposed to?

I am trying to make a GUI for a game. I am very new to Java, especially GUI. The code below is a snippet which is supposed to make a JFrame with nested panels for organization. It works until I add buttons to the button panel. They end up on the boardBckg panel. If I manage to place them on the correct panel the JTextField disappears or it takes up the entire screen. I have been working on this part of the code for the past two days and I could really use GUI tips.
private void makeWindow()
{
boardPanel = new JPanel();
boardBckg = new JPanel();
menuPanel = new JPanel();
save = new JButton("Save");
save.setSize(Buttons);
load = new JButton("Load");
load.setSize(Buttons);
replay = new JButton ("Replay");
replay.setSize(Buttons);
words = new JTextField();
frame = new JFrame(title);
boardPanel.setSize(PANEL);
boardPanel.setMaximumSize(MAX);
boardPanel.setMinimumSize(MIN);
boardPanel.setLayout(new GridLayout(m,n));
boardBckg.setSize(1000, 1000);
boardBckg.setBackground(Color.cyan);
boardBckg.add(boardPanel, BorderLayout.CENTER);
frame.setSize(1500, 1000);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoxLayout vertical = new BoxLayout(menuPanel, BoxLayout.Y_AXIS);
menuPanel.setSize(500, 1000);
menuPanel.setBackground(Color.blue);
menuPanel.setLayout(vertical);
frame.add(boardBckg);
frame.add(menuPanel);
JPanel iGiveUp = new JPanel();
iGiveUp.setBackground(Color.black);
JPanel buttons = new JPanel();
buttons.setBackground(Color.darkGray);
buttons.add(save);
buttons.add(load);
buttons.add(replay);
menuPanel.add(iGiveUp);
menuPanel.add(buttons);
iGiveUp.add(words);
boardBckg.add(boardPanel, BorderLayout.CENTER);
The default layout of a JPanel is the FlowLayout. You can't just specify a BorderLayout constraint when you add the component to the panel.
frame.add(boardBckg);
frame.add(menuPanel);
The default layout for (the content pane of) the frame is a BorderLayout. If you don't specify a constraint, then the component is added to the BorderLayout.CENTER. Problem is only one component can be added to the CENTER so you only see the last comoponent added.
frame.setVisible(true);
Component should be added to the frame BEFORE the frame is packed and made visible. So the above statement should be the last statement in your constructor.
I have no ideas what your desired layout is but you need to start with something simple and take advantage of the default BorderLayout of the frame.
So your basic logic might be something like:
JPanel menuPanel = new JPanel()
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
menuPanel.add(...);
menuPanel.add(...);
JPanel center = new JPanel();
center.setLayout(...);
center.setBackground( Color.BLUE );
center.add(...);
frame.add(menuPanel, BorderLayout.LINE_START);
frame.add(center, BorderLayout.CENTER);
frame.pack();
frame.setVisible( true );
The main point is to break the panels down logically and add them to the frame one at a time. So first get the menu and its child components added to the frame is the correct position. Then you can add the CENTER panel and its child components.

Create a main program with two tabs from two different JFrame classes [duplicate]

I Have an already given JFrame frame, that I want to show it (insert it) into a tab of JTabbedPane, but that was not possible explicitely like that:
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainTabs.addTab("Editor", null, frame, null);
And the error was:
java.lang.IllegalArgumentException: adding a window to a container
I tried some solutions like to insert the frame into a JPanel but also in vain. I have the idea to convert it into an InternalJFrame but I don't have any idea about that.
Is that any solution to insert that frame?
UPDATE:
I tried that soluion:
mainTabs.addTab("Editor", null, frame.getContentPane(), null);
But i lost the JMenuBar that I added.
You can't add JFrame(or another top-level component) to another component/container, but you can use getContentPane() method of frame, to get main panel of your frame and add that to JTabbedPane tab. Like next:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
frame.add(new JButton("button"));
tabs.addTab("1", frame.getContentPane());
Also you can change JFrame to JPanel and use that.
Read about JInternalFrame, top-level containers.
EDIT: getContentPane() doesn't return any decorations or JMenuBar, this components you need to add manually, like in next example with menu:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
JMenuBar bar = new JMenuBar();
bar.add(new JMenu("menu"));
frame.setJMenuBar(bar);
frame.add(new JButton("button"));
JPanel tab1 = new JPanel(new BorderLayout());
tab1.add(frame.getJMenuBar(),BorderLayout.NORTH);
tab1.add(frame.getContentPane());
tabs.addTab("1", tab1);

What is a Java NullPointerException? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I created a Java Application and get this Exception:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Executer.<init>(Executer.java:21)
at Executer.main(Executer.java:14
Here is the code:
import javax.swing.*;
import java.awt.*;
public class Executer {
private JLabel lblCommand;
private JTextField txtEnter;
private JButton btNext, btPrevious;
private JPanel panel;
public static void main(String[] args) {
new Executer();
}
public Executer() {
JFrame frame = new JFrame("Execute Script");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,400);
frame.setVisible(true);
frame.add(panel);
frame.setVisible(true);
MyPanel();
Text();
Buttons();
Fields();
}
public void MyPanel() {
panel = new JPanel();
panel.setLayout(null);
}
public void Text(){
lblCommand = new JLabel("Enter Here");
lblCommand.setBounds(135, 50, 150, 20);
Font styleOne = new Font("Arial", Font.BOLD, 13);
lblCommand.setFont(styleOne);
panel.add(lblCommand);
}
public void Fields () {
txtEnter = new JTextField();
txtEnter.setBounds(210, 50, 150, 20);
panel.add(txtEnter);
}
public void Buttons() {
btNext = new JButton ("Next");
btNext.setBounds(380,325,100,20);
panel.add(btNext);
btPrevious = new JButton ("Previous");
btPrevious.setBounds(260,325,100,20);
panel.add(btPrevious);
}}
What is a NullPointerException? How would I find out?
You need to instantiate panel before adding it. If you use panel before calling MyPanel(), panel is still null, hence the NullPointerException.
While you're here, give this a glance. http://geosoft.no/development/javastyle.html
Method names in Java should be mixed case starting with a lower case letter, e.g. myPanel() instead of MyPanel(). To most of us, MyPanel() looks like a constructor at first glance because you improperly styled it.
Additionally, MyPanel, Text, Fields, and Buttons should all be private methods, as it would be improper for an external class to call them.
Problem in this line frame.add(panel); Panel is not initialized at that point, move this line MyPanel(); before adding to initialize it.
As others have said, you need to create the JPanel before you try to add it to the JFrame. In fact, you should typically create all components inside the JPanel as well. I suggest that you move the calls to
Text();
Buttons();
Fields();
from the Executer constructor to the MyPanel() method and call MyPanel() before calling frame.add(panel);.
In addition, you do not need to call frame.setVisible(true); twice. Also, you should use a LayoutManager rather than calling panel.setLayout(null);. See the Oracle tutorial for Using Layout Managers.
Yes you are adding panel to the frame before creating object of JPanel. Anyway change your constructor with this:
public Executer() {
JFrame frame = new JFrame("Execute Script");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 400);
MyPanel();
Text();
Buttons();
Fields();
frame.add(panel);
frame.setVisible(true);
}
thanks.

JFrame Button Logic error

I've been spending some time relearning java and a peculiar logic error hit me here.
import javax.swing.*;
import java.awt.*;
class Frame
{
public static void main (String args[])
{
JFrame frame = new JFrame("Tester Frame");
frame.setSize(400, 500);
JButton btn1 = new JButton("FOO");
btn1.setSize(150, 50);
btn1.setLocation(45, 0);
JButton btn2 = new JButton("BAR");
btn2.setSize(150, 50);
btn2.setLocation(205, 0);
Container content = frame.getContentPane();
content.setBackground(Color.blue);
content.add(btn1);
content.add(btn2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}//end main
}
I've created 2 JButton objects, and they should be the same size, with different location and text. This of course is not the case, the "FOO" button is exactly where and how I want it to be, but the "BAR" button is the size of the entire frame.
Help!
1) You are attempting to use Absolute LayoutManager via setSize and setLocation etc, but without calling setLayout(null) on the component you are adding the JButtons to. However this is not a best practice in Swing.
When adding to JFrame contentpane default layout is BorderLayout which adds components to is default position of BorderLayout.CENTER.
Have a read on A Visual Guide to Layout Managers
2) Also when using a correct LayoutManager you would omit JFrame#setSize(..) call and replace it with JFrame#pack() before setting the JFrame visible.
3) Also have a read on Concurrency in Swing specifically on The Event Dispatch Thread
which dictates all swing components be created on EDT via SwingUtillities.invokeXXX(..) block:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//create and manipulate swing components here
}
});
4) Also rather use JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); as this will allow any other threads timers etc to carry on execution even after JFrame has been disposed.
add:
frame.getContentPane().setLayout(null);
To your code after the line:
frame.setSize(400, 500);
Components added to a container are tracked in a list. The order of the list will define the components' front-to-back stacking order within the container. If no index is specified when adding a component to a container, it will be added to the end of the list (and hence to the bottom of the stacking order).In your code the buttons are stacked over the other.That is why you get this Error(as you think it is).
This will solve your problem:-
import javax.swing.*;
import java.awt.*;
class OP3
{
public static void main (String args[])
{
JFrame frame = new JFrame("Tester Frame");
frame.setSize(400, 500);
JButton btn1 = new JButton("FOO");
btn1.setSize(150, 50);
btn1.setLocation(45, 0);
JButton btn2 = new JButton("BAR");
btn2.setSize(150, 50);
btn2.setLocation(205, 0);
JPanel p = new JPanel(new FlowLayout());
p.add(btn1);
p.add(btn2);
frame.add(p);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}//end main
}
Just add a panel to the frame and add the buttons to the panel.
import javax.swing.*;
import java.awt.*;
class source
{
public static void main (String args[])
{
JFrame frame = new JFrame("Tester Frame");
frame.setSize(400, 500);
JPanel panel=new JPanel();//panel added here
panel.setSize(frame.size());
panel.setLocation(0, 0);
JButton btn1 = new JButton("FOO");
btn1.setSize(150, 50);
btn1.setLocation(45, 0);
JButton btn2 = new JButton("BAR");
btn2.setSize(150, 50);
btn2.setLocation(205, 0);
panel.add(btn1);
panel.add(btn2);
Container content = frame.getContentPane();
content.setBackground(Color.blue);
content.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}//endmain

A Simple GUI Interface for Java

doing some self study for a simple GUI interface for Java. Tried to code a simple interface. Here is the below code:
public void MainPanel() {
JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane mpt = new JTabbedPane();
mpt.addTab("Intro", new IntroPanel());
mpt.addTab("Catalogue", new CataloguePanel());
mpt.addTab("Order", new OrderPanel());
mpt.addTab("Track", new TrackPanel());
JPanel main = new JPanel();
main.setBackground(Color.white);
JLabel label1 = new JLabel("Intro");
main.add(label1);
frame.add(main);
frame.add(mpt);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
The problem I am currently facing is: If I put frame.add(main) infront of frame.add(mpt), only the tabs will appear but the the label intro. If I put frame.add(mpt) infront of frame.add(main). Intro will appear but not the tabs.
Why is this happening? Why are they overlapping each other? I did it the same way as some tutorial but to no avail..
Try giving it some other layout.
eg. frame.setLayout(new FlowLayout());
Info:
According to Java Naming Conventions your method's name should be like public void mainPanel()

Categories

Resources