Java NullPointer Exception with Jpanel [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Hello guys I started a personal project to where I would basically make a meal planner based of how many calories a person wants to consume per day for myself. I know it maybe a tough challenge for me since I'm a beginner, but I can do it! I will leave the backend later, but for now I wanted to work on the UI and I'm having trouble with this piece of code. I want to make a list of panels to be held in a panel, based on how many number of meals they want is how many panels will appear. any insight on this will be much appreciated.
package mealplanner;
import javax.swing.*;
/**
* Created by Usman on 6/8/2017.
*/
public class MealPlannerPanel extends JPanel {
JPanel[] panel;
int mealsPerDay, caloriesPerDay;
public MealPlannerPanel(){
mealsPerDay = Integer.parseInt(JOptionPane.showInputDialog("How many meals would you like per day?",null));
caloriesPerDay = Integer.parseInt(JOptionPane.showInputDialog("What is your daily calorie aim?",null));
panel = new JPanel[mealsPerDay];
for(int i = 0; i < panel.length; i++){
add(panel[i]);
}
}
public static void main(String[] args){
MealPlannerPanel planner = new MealPlannerPanel();
JFrame frame = new JFrame("Meal Planner");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(planner);
frame.pack();
frame.setVisible(true);
}
}

panel = new JPanel[mealsPerDay];
That statement just creates an array that is able to hold the given number of panels.
It does not actually create the panel (so when you index into the array you will get a null object).
So you need something like:
for(int i = 0; i < panel.length; i++){
JPanel onePanel = new JPanel();
panel[i] = onePanel;
add(panel[i]);
}
Also, be more descriptive with you variable names. "panel" implies a single component. Given it is meant to represent an array the least you can do is call it "panels" so we know there is more than one.

Related

How to create multiple buttons that do the same on different positions without creating actual new buttons?

Im really new to java and Programming as a whole. In school, I decided to work on a project to raise my grade, and as simple java is currently our topic i decided on trying to recreate battleships in a jframe using swing. I've made some good progress so far but im stuck on a quality of Life Problem.
So basically, in the editor we use (Java Editor ( javaeditor.org )) i use swing to implement buttons etc. in the jframe. As im gonna need a lot of Buttons for the games gui, I want to do it, so i dont have to make several buttons which have to be filled in with the arguments. What im trying to do is have some arguments create several buttons for me so they dont actually all need their own “method“ as all buttons have to basically do the exact same thing.
I tried searching for similar things on google but I couldnt find anything, so i decided to create this account to ask if someone might be able to help me with this Problem. If something isnt understandable feel free to ask (English isnt my mother tongue so some parts might be hard to understand).
Looking forward to any replies! Thanks in advance for helping.
Initially I thought I could use a for-loop to create these multiple buttons but there would always be some kind of error with the ActionPerformed argument.
for (int i = 0;i > 25;i++ ) {
jButton[i].setBounds(48, 48 + i, 113, 73);
jButton[i].setText("jButton1");
jButton[i].setMargin(new Insets(2, 2, 2, 2));
jButton[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton[i]_ActionPerformed(evt);
}
});
cp.add(jButton[i]);
}
As already said I expect there to be more than 1 button, whilst keeping the lines of code in the constructor as short as possible.
If you're desiring a grid of buttons, then create them in a for loop, and place them in the JPanel using a GridLayout. Something like this might work:
import java.awt.GridLayout;
import javax.swing.*;
#SuppressWarnings("serial")
public class ManyButtons extends JPanel {
private static final int SIDES = 10;
private JButton[][] buttonGrid = new JButton[SIDES][SIDES];
public ManyButtons() {
setLayout(new GridLayout(SIDES, SIDES));
for (int row = 0; row < buttonGrid.length; row++) {
for (int col = 0; col < buttonGrid[row].length; col++) {
String text = String.format("[%d, %d]", col + 1, row + 1);
buttonGrid[row][col] = new JButton(text);
buttonGrid[row][col].addActionListener(event -> {
String command = event.getActionCommand();
System.out.println("Button pressed: " + command);
});
add(buttonGrid[row][col]);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
ManyButtons mainPanel = new ManyButtons();
JFrame frame = new JFrame("Many Buttons");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
Also avoid setBounds and null layouts.
Regarding:
.... without creating new JButtons
This is not technically possible if you want a fully functioning button (as opposed to creating a rendered button in a JTable), however, buttons can share ActionListeners, so all buttons that do the same kind of thing (think -- all the number buttons on a calculator) can use the very same ActionListener. Alternatively, you can set a button's Action using your own class that extends from AbstractAction for even greater flexibility and power, and multiple buttons (and JMenuItems) can share the same action.

Use result from if statement in another if statement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am writing a quiz app, where you get marks as you answer the correct questions and your score increases, and I have to use if statements. Please does any one know how to use a value in an if statement in another if statement! I'm kinda confused about it and its hooking me up at work here....Thanks for the help!... here is a little code example;
int x = 3;
String xy = Integer.toString(x);
int y = 0;
String yy = Integer.toString(y);
JButton one = new JButton ("Quest 1");
one.addActionListener (new ActionListener (){
public void actionPerformed(ActionEvent p) {
JFrame ex = new JFrame ();
ex.setTitle("Question 1);
ex.setSize(400, 400);
ex.setLayout(new FlowLayout());
ex.setBackground(Color.WHITE);
JLabel ey = new JLabel ("What is the capital of Japan?);
Font tan = new Font ("Script MT Bold", Font.BOLD, 18);
ey.setFont(tan);
ey.setForeground(Color.BLACK);
ex.add(ey, BorderLayout.NORTH);
JButton answ = new JButton("submit");
JTextField g = new JTextField (10);
g.setFont(tan);
String ans = "Tokyo";
String merit = "Correct";
String flop = "wrong";
String mer = merit + ans;
String flip = flop + ans;
answ.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent p) {
if (g.getText.equals("Tokyo") {
JOptionPane.showMessageDialog(null, mer);
one.setText(xy);
}
else {
JOptionPane.showMessageDialog(null,flip);
one.setText(yy);
}
//In my next Action Listener, I would love to
//pick the score from the previous listener....and add to the next score....
//So that we have something like ....
//x(updated from previous listener) + x
ex.add(g, BorderLayout.SOUTH);
}
});
}
});
The only problem I can guess at in the code supplied is that you're testing if a JTextField's text contains a specific String, "Tokyo" in your GUI creational code. This is code that runs at GUI creation and before the user has had any chance to enter data. To fix this, the if test should be within some listener, perhaps a JButton's ActionListener. Otherwise I have no idea what you mean by if within an if.
Edit
Regarding your new information:
I am writing a quiz app, where you get marks as you answer the correct questions and your score increases, and I have to use if statements.
You need to completely re-design your code as you're hard coding your code logic within the GUI, making for a very rigid, huge, and difficult to enhance program (as you're finding out) since the code logic must change as the state of the program changes.
Instead you should split out your program logic, the "model" from the GUI, the "view", and try to create them and test them independently, something similar to (or equal to) a "Model-View-Controller" or "MVC" program design. Start with the model, the "guts" of the program and create your non-GUI Question class, one with instance fields, methods, and any other supporting classes. Once this has been tested and debugged, then try to create a GUI or view class that can use this model and display its state. You might also want to create a "Controller" class with listeners that help connect the view to the model.
For example, if your quiz is to be a multiple-choice type of program, then consider:
A Question class that contains the question String, possible answer Strings and the correct answer String.
Give it a public boolean test(String testString) that returns true if the correct answer String is passed into it.
Allow the Question class to randomize the order of the possible answer Strings, likely held in an ArrayList.
Then create a Quiz class that holds an ArrayList of Questions.
Then create a GUI to display these.
I generally create GUI's that are geared to create JPanels, not JFrames for increased flexibility and then create the JFrame when needed.
Create a QuestionPanel that displays the question String and the randomized possible answer Strings.
Display the possible answers as JRadioButtons with a ButtonGroup to limit the selection to one.
etc....
You'll also want a class to read from a text file data for each question, and load that data into the Quiz class.
You'll also want a mechanism to grade.
Please make all required variables as class level variables instead of declaring it in actionlistner method. Class level variables will be visible in all methods so no need to pass those. Declare score variable as class level.
public class ClassTest {
int score=0;
public void acgionlistner1(Event ev)
{
if(ans.equals(userinput))
{
score++;
}
}
public void acgionlistner2(Event ev)
{
if(ans.equals(userinput))
{
score++;
}
}
.
.

JFrame issue. This keeps printing double the amount of windows I need

I have two more classes that this main class is using, but I think that it can be answered without those classes because I think it is a logic problem.
I'm trying to create a JFrame that prints out a drawing. The way the API is set up, I want public Viewer to create the frame and set the title and then main to instantiate a viewer. But the way I have this set up keeps printing double the amount of frames that I need. Also, when I try to concatenate
v.setTitle(v.getTitle() + String.format(" pi = %.4f", pi));
it doesn't work. It just prints a new JFrame with just
String.format(" pi = %.4f", pi));
and the original JFrame prints its name (two separate frames). So I know it has to be a logic problem somewhere, but I can't figure it out.
import java.util.Scanner;
import javax.swing.JFrame;
public class Viewer extends JFrame{
private ControlPanel cp;
public Viewer(String name, int in){
JFrame frame = new JFrame();
frame.setSize(550, 550);
frame.setTitle("Name: " + name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cp = new ControlPanel(in);
frame.add(cp);
frame.setVisible(true);
}
/**
* #param args
*/
public static void main(String[] args) {
int n = (int) 1e4;
System.out.println("Enter the number of runs to make <1 to 4>:");
#SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
int runs = in.nextInt();
if (runs > 4){
runs = 4;
}
if (runs < 1){
runs = 4;
}
for (int i = 1; i <= runs; i ++){
n = n * runs;
Viewer v = new Viewer("Trey Wilson", n);
int hits = v.cp.getHits();
double pi = 4.0 * hits / n;
v.setTitle(v.getTitle() + String.format(" pi = %.4f", pi));
v.pack();
v.setVisible(true);
v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
Your code has multiple errors:
It extends JFrame and creates a JFrame object (just remove the extends JFrame from the class, it's recommended to do so)
Here lies your problem:
Viewer v = new Viewer("Trey Wilson", n);
this calls the Viewer's class constructor but the constructor itself sets it's own title, size, etc, and then you're modifying those attributes and making another call to setVisible(true) which makes this last frame visible again, so remove one or the other.
You're not placing your program on the EDT, see SwingUtilities.invokeLater() why is it needed?
See The use of multiple JFrames, Good / Bad Practice? (Bad) you should instead want to try using a single JFrame with multiple JPanels switching them with a Card Layout or using JDialogs to display / retrieve information to / from user
From your comment above:
I'm completely new to Jframe (along with java) and was given an API to follow along with. What does extends mean? I'll give it a google, but didn't know if you had an answer on how to fix it.
I recommend you to learn the OOP basics, Java concepts first before going into Java Swing which will make your learning harder because it will add more complexity to it and make it harder to you to maintain or make a quality software.
THIS SOLVES THE PROBLEM OF DOUBLE FRAME APPEARING:
So you can either take away frame.setVisible(true); in the constructor as in:
public Viewer(String name, int in){
JFrame frame = new JFrame();
frame.setSize(550, 550);
frame.setTitle("Name: " + name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cp = new ControlPanel(in);
frame.add(cp);
}
OR Rather take away v.setVisible(true); from you main method.
I will advice you to take away frame.setVisible(true);. But depending on you requirement, choose the best for yourself.

Java Swing GridBagLayout adding components inbetween other components

What i want to achieve: I want to be able to add components to a scrollpane inbetween components already added. In other words, i somehow want to move the index up and plop some new components where the old ones used to be. I created a short program to illustrate my problem, it replace the components it doesnt add them. can someone help?
Why i want to achieve it (to answer the inevitable question related to this, this is long only read if interested): Ive got a scrollpane in which i need to display lots of components, approx 150000 JTables. This takes too much memory and processing time. So what ive done is to load the scrollpane with 150000 TextFields and only displaying 100 of the tables at a time. If you scroll to textfield 200 it loads the next 100 tables, replacing the previously created text fields with the tables. If you move to textfield 300 it loads the next 100 tables, and removes the first 100, again replacing it with textfields. My thinking here is that the textfields are going to be much less resource intensive than the tables, and so far this thinking seems to be correct. So this all seems to work pretty well (on my smaller test file - about 5000 records). The problem is that to create the 150000 textfields also takes lots of time. I thought of a solution for this. To not create a textfield for every table, but to rather create 1 textfield for every 100 tables. The idea is that when you scroll to textfield 2 (instead of 200), it loads the next 100 tables, replacing textfield nr 2 with tables nr 200 - 299. So somehow i need to move textfield 3 and all those below it down in order to insert all the tables. Phew, hope this makes sense
So looking at the example below what i effectively want to achieve is to replace say textfield 20 with 5 new textfields, but not repalce textfields 21 - 25.
import javax.swing.*;
import java.awt.*;
public class Example {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ExampleScreen ex = null;
ex = new ExampleScreen();
ex.setVisible(true);
}
});
}
private static class ExampleScreen extends JFrame{
GridBagConstraints gc = new GridBagConstraints();
JPanel panel = new JPanel(new GridBagLayout());
JScrollPane scrollPane = new JScrollPane(panel);
public ExampleScreen() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(scrollPane, BorderLayout.CENTER);
setupFields();
createExtraFieldsInbetween(20);
this.pack();
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
scrollPane.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);
}
//after this happens i need to add components somewhere in the middle, like after textfield 20 for example
private void setupFields() {
gc.gridy = 0;
gc.gridx = 0;
for(int k = 0; k < 50; k++) {
JTextField textField = new JTextField();
textField.setPreferredSize(new Dimension(1500,36));
textField.setText(new Integer(k).toString());
panel.add(textField, gc, k);
gc.gridy++;
}
}
//this is to create extra fields inbetween those already created
//this does not work, it overwrites the existing components, doesnt move them up
private void createExtraFieldsInbetween(int i) {
gc.gridy = i;
//create 5 extra
for(int k = i; k < i + 5; k++) {
JTextField textField = new JTextField();
textField.setPreferredSize(new Dimension(1500,36));
textField.setText("Extra Field : " + new Integer(k).toString());
panel.add(textField, gc, k);
gc.gridy++;
}
}
}
}
Anytime I see a number like 150,000 I think the user can't be bothered to scroll through all those components so you need a better UI.
Anyway if you have a problem with a GridBagLayout, then don't use a GridBagLayout.
It looks to me like you are just displaying the components vertically so try a BoxLayout. Then you can just use the add(...) method to specify the index where you want to remove/add components.

Write a 2D Array on a JFrame java

So i was wondering I'm new to java but I know my way around it but I wanted to make a 2d tile game.
Now I heard that you can do this with a 2d Array to make the map.
But how do you make the map appear on the screen, JFrame, as pictures?
So an example of the array/map here:
1111111111
1011011001
1001100011
0000100011
0000000000
2222222222
0 = blueSky.png
1 = cloud.png
2 = grass.png
Thanks!
EDIT 2
So i have now this:
import javax.swing.*;
import java.awt.*;
public class Game extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
ImageIcon sky = new ImageIcon ("/Users/pro/Desktop/sky.png");
JPanel grid = new JPanel();
grid.setLayout(new GridLayout(25, 25));
for (int i = 0; i < 25; i++) {
for (int n = 0; n < 25; n++) {
grid.add(new JLabel(sky));
}
}
JFrame frame = new JFrame("Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setPreferredSize(new Dimension(640, 400));
frame.add(grid);
frame.pack();
frame.setVisible(true);
}
}
this prints some tiles with the sky picture but how do i make the bottom row an other picture?
I'd consider the 2D array as the non-GUI model, that the data itself would likely be held in some data file, perhaps a text file, that there would be methods for reading the data in to be held by a 2D array of perhaps ints, perhaps custom Cell classes (again, still all non-GUI). Then the GUI would have to interpret the model and display the data in some logical way. This could perhaps be done by creating a 2D grid of JLabels held by a JPAnel that uses GridLayout, and then use ImageIcons to hold the images, and set the icon of each JLabel based on the state of the model.
Edit 1
So possible classes used include:
TileType: an enum that associates the tile concept with the numbers held by the data file
TileCell: non-GUI class, holds a TileType field, also may hold a List of items that can be found on the cell (if the game needs this). May have information about its neighbors.
TileCellGrid: non-GUI class holding a 2D grid of TileCells.
GridDataIO: utility class to read in and write out grid data to a file.
GameGrid: GUI class that would hold a GridLayout using JPanel that holds JLabels whose ImageIcons display the images you list in your OP.
Edit 2
regarding your question:
Alright how can i set the right picture for everyLabel ?
I would use an observer/observable pattern and add a listener to the model. Whenever the model changes it should thus notify the GUI or view. Then the view would request the data array, would iterate through it and would change the image icons that need changing as it loops through the array.
I suggest you use JLabels with icons and lay them out using GridLayout.
Related question / answer with sample code and screen shot:
Tiled images in swing

Categories