This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Im getting this strange NullpointerException whilst adding a JLabel to a JPanel:
loadoutAdvWeaponPanels = new JPanel[4][4];
loadoutAdvWeaponButtons = new JButton[4];
loadoutAdvPistolLabels = new JLabel[4][8];
//Init loadoutAdvPanels[0]
loadoutAdvWeaponButtons[0] = new JButton("Pistols");
loadoutAdvPistolLabels[0][0] = new JLabel("USP-S");
loadoutAdvPistolLabels[0][1] = new JLabel("P2000");
loadoutAdvPistolLabels[0][2] = new JLabel("Dual Berettas");
loadoutAdvPistolLabels[0][3] = new JLabel("P250");
loadoutAdvPistolLabels[0][4] = new JLabel("Five-SeveN");
loadoutAdvPistolLabels[0][5] = new JLabel("CZ75-Auto");
loadoutAdvPistolLabels[0][6] = new JLabel("Desert Eagle");
loadoutAdvPistolLabels[0][7] = new JLabel("R8 Revolver");
loadoutAdvWeaponPanels[0][0].add(loadoutAdvPistolLabels[0][0]);
The error occurs in the last line, but i dont know why.
Simple as I see!
You do not initialisized loadoutAdvWeaponPanels[0][0]
I prefer to use this:
for(int i = 0; i < loadoutAdvWeaponPanels.length; i++) {
for(int j = 0; loadoutAdvWeaponPanels[i].length; j++) {
{
loadoutAdvWeaponPanels[i][j] = new JPanel();
}
}
you should initialize the array loadoutAdvWeaponPanels
loadoutAdvWeaponPanels = new JPanel[4][4];
for(int i = 0; i < loadoutAdvWeaponPanels.length; i++)
for(int j = 0; j < loadoutAdvWeaponPanels[i].length; j++)
loadoutAdvWeaponPanels[i][j] = new JPanel();
or just initialize what you need
loadoutAdvWeaponPanels[0][0] = new JPanel();
loadoutAdvWeaponPanels[0][0].add(loadoutAdvPistolLabels[0][0]);
Related
I am trying to create GUI like given in first picture, but I am not able to do it.here is the image
I am getting only one combo1, combo2, combo3 and serialNoLabel instead of 5 [5 is the size of list]
ArrayList<String> list; // the size of the list is 5
JComboBox combo1[] = new JComboBox[list.size()];
JComboBox combo2[] = new JComboBox[list.size()];
JComboBox combo3[] = new JComboBox[list.size()];
JLabel SerialNoLabel[] = new JLabel[list.size()];
JPanel masterPanel[] = new JPanel[list.size()];
JDialog masterDialog = new JDialog();
masterDialog.setVisible(true);
masterDialog.setSize(800, 500);
masterDialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
masterDialog.setVisible(true);
for(int j =0; j < list.size(); j++) {
masterPanel[j] = new JPanel();
SerialNoLabel[j] = new JLabel(list.get(j));
masterPanel[j].add(SerialNoLabel[j]);
combo1[j] = new JComboBox();
masterPanel[j].add(combo1[j]);
combo2[j] = new JComboBox();
masterPanel[j].add(combo2[j]);
combo3[j] = new JComboBox();
masterPanel[j].add(combo3[j]);
masterDialog.add(masterPanel[j]);
masterDialog.revalidate();
}
I believe it's a layout issue leading your masterPanels to be on top of each other.
So I would do something like this:
JPanel mainPanel = new JPanel();
FlowLayout experimentLayout = new FlowLayout();
mainPanel.setLayout(experimentLayout);
for(int j =0; j < list.size(); j++) {
masterPanel[j] = new JPanel();
SerialNoLabel[j] = new JLabel(list.get(j));
masterPanel[j].add(SerialNoLabel[j]);
combo1[j] = new JComboBox();
masterPanel[j].add(combo1[j]);
combo2[j] = new JComboBox();
masterPanel[j].add(combo2[j]);
combo3[j] = new JComboBox();
mainPanel.add(masterPanel[j]);
}
Of course you could other layouts as well. But I believe you want to go for a FlowLayout. See the documentation about FlowLayout here.
You can learn more about other layouts here
I make some variable by
String[] StoreValueFromTable = new String[5];
String[] ColumName = new String[5];
for(int Count=0;Count<5;Count++){
StoreValueFromTable[Count] = "QueryTechnica"+Count;
ColumName[Count] = "QT"+Count;
System.out.println(StoreValueFromTable[Count]+", "+ColumName[Count]);
}
I am trying to make JTextField[] Fiels[Count] = new JTextField(); it's giving me error. I am not sure, is there a way to make it dynamically..
You can do this:
final int TEXTFIELDS_COUNT = 5;
// Create an array of 5 JTextFields
JTextField[] fields = new JTextField[TEXTFIELDS_COUNT];
for(int count = 0; count< TEXTFIELDS_COUNT; count++){
// fields[count] represents a single JTextField
fields[count] = new JTextField();
// Do something with fields[count], like setting its text
// fields[count].setText("some text");
StoreValueFromTable[count] = "QueryTechnica"+count;
ColumName[count] = "QT"+count;
System.out.println(StoreValueFromTable[count]+", "+ColumName[count]);
}
How am I able to insert the i from the for loop in the imageview below, not the i to replace 12 in the file name so that it can become;
ImageView Moon_img12 = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/i.png")));
Code:
for(int i=0; i<=total_donnation; i++)
{
ImageView Moon_img12 = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/12.png")));
}
Try
ImageView[] moon_images = //init array
//loop
ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/"+i+".png
moon_images[i]= Moon_img;
Thanks you all, i have solved it now using your help.
GridPane grid = new GridPane();
int max_columns = 5;
int current_row = 0;
int current_column = 0;
int total_donnation = 7;
for(int i=1; i<=total_donnation; i++)
{
ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/"+i+".png")));
grid.add(Moon_img, current_column,current_row);
current_column = current_column+1;
if (current_column == max_columns )
{
current_row = current_row+1;
current_column = 0;
}
}
return grid;
i have this piece of code
ImageIcon[] Image = {
new ImageIcon("../KingGame/src/game/img/1.gif"),
new ImageIcon("../KingGame/src/game/img/2.gif"),
new ImageIcon("../KingGame/src/game/img/3.gif"),
new ImageIcon("../KingGame/src/game/img/4.gif"),
new ImageIcon("../KingGame/src/game/img/5.gif"),
new ImageIcon("../KingGame/src/game/img/6.gif"),
new ImageIcon("../KingGame/src/game/img/7.gif"),
new ImageIcon("../KingGame/src/game/img/8.gif"),
new ImageIcon("../KingGame/src/game/img/9.gif"),
};
an d i tried with the code below replace the script above
ImageIcon image[] = new ImageIcon[9];
for (int i = 1; i < image.length; i++) {
new ImageIcon("../KingGame/src/game/img/"+i+".gif");
}
but the result is...any image is loaded. what is the error?
thanks
You forgot to put new images into array:
image[i] = new ImageIcon("../KingGame/src/game/img/"+i+".gif");
Now it does the same thing as your old code.
It should be
for (int i = 0; i < image.length; i++) {
image[i] =new ImageIcon("../KingGame/src/game/img/"+(i+1)+".gif");
}
And now a very easy java question....
Object[] objectList={
new Object(name[0], description[0], R.drawable.creep_0),
new Object(name[1], description[1], R.drawable.creep_1),
new Object(name[2], description[2], R.drawable.creep_2),
new Object(name[3], description[3], R.drawable.creep_3),
};
How can i do this dynamically with a for cycle? Thanks!
Creep[] creeps = new Creep[]
{ R.drawable.creep_0, R.drawable.creep_1, R.drawable.creep_2,
R.drawable.creep_3 };
Object[] objectList = new Object[4];
for (int i = 0; i < 4; i++) {
objectList[i] = new Object(name[i], description[i], creeps[i]);
}
ArrayList objectList = new ArrayList();
for (int i = 0; i < 4; i++)
{
Object o = new Object(name[i], description[i], R.drawable.creep_i);
objectList.Add(o);
}