Hi guys, I really need some help with this, I've spent hours looking for a solution.
I have 4 classes of which 2 are important right now.
1. The user enters his username in the JTextField userF, if I print that userF.getText() in the class where userF is created I get the username.
2. I then want to get the username in another class but it gives me NullPointerException - so userF goes empty again.
3. I then print userF in class 1 again and it works ?!?!
I know it has something to do with parameterizing but I can't seem to figure out what parameterizing actually means.
Here is some code from the first class (I left out a lot that I think you don't need)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Interface implements ActionListener
{
private JFrame frame, wrongPasswordFrame, userNotRegFrame;
private JPanel pan1, pan2, pan3, pan4, pan5, pan6;
private JComboBox toBox, fromBox;
private JLabel title, userL, nameL, surL, genL, passL;
private JTextField userF, nameF, surF, ageF, natF, genF, picF, eventNameF, eventPicF, infoF, timeF, dateF, addressF;
private JPasswordField passF;
private JButton loginButH, loginButL, signButH, backBut, signButS, logout, createEvent, events, messages, walls, users, tryAgain, exit;
private JFileChooser browser;
public Interface()
{
}
public void create()
{
frame = new JFrame();
frame.setLayout(new GridLayout(2,1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.white);
frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
frame.setTitle("SNL Home");
Font font1 = new Font("SansSerif", Font.BOLD, 20);
Font font2 = new Font("Hobo Std", Font.PLAIN, 100);
Font font3 = new Font("SansSerif", Font.PLAIN, 30);
browser = new JFileChooser();
browser.setControlButtonsAreShown(false);
pan1 = new JPanel();
pan1.setLayout(new GridLayout(1,1));
pan1.setBackground(Color.white);
pan2 = new JPanel();
pan2.setLayout(new GridLayout(1,2));
pan2.setBackground(Color.white);
pan3 = new JPanel();
pan3.setBackground(Color.white);
pan4 = new JPanel();
pan4.setBackground(Color.white);
pan5 = new JPanel();
pan5.setBackground(Color.white);
pan6 = new JPanel();
pan6.setBackground(Color.white);
title = new JLabel (new ImageIcon ("SocialNetworkingLiveLabel.jpg"));
userL = new JLabel ();
passL = new JLabel ();
userF = new JTextField(15);
userF.setFont(font1);
nameF = new JTextField(15);
nameF.setFont(font1);
loginButH = new JButton();
loginButH.setIcon(new ImageIcon("Login.jpg"));
loginButH.addActionListener(this);
loginButH.setCursor(new Cursor(Cursor.HAND_CURSOR));
loginButH.setBackground(Color.white);
loginButH.setBorderPainted(false);
loginButL = new JButton();
loginButL.setIcon(new ImageIcon("Login2.jpg"));
loginButL.addActionListener(this);
loginButL.setCursor(new Cursor(Cursor.HAND_CURSOR));
loginButL.setBackground(Color.white);
loginButL.setBorderPainted(false);
backBut = new JButton();
backBut.setBackground(Color.white);
backBut.addActionListener(this);
backBut.setCursor(new Cursor(Cursor.HAND_CURSOR));
backBut.setBorderPainted(false);
signButH = new JButton();
signButH.setIcon(new ImageIcon("Sign Up.jpg"));
signButH.addActionListener(this);
signButH.setCursor(new Cursor(Cursor.HAND_CURSOR));
signButH.setBackground(Color.white);
signButH.setBorderPainted(false);
signButS = new JButton();
signButS.setIcon(new ImageIcon("SignUp2.jpg"));
signButS.addActionListener(this);
signButS.setCursor(new Cursor(Cursor.HAND_CURSOR));
signButS.setBackground(Color.white);
signButS.setBorderPainted(false);
}
public void displayHomePage()
{
frame.remove(pan3);
frame.remove(pan4);
frame.remove(pan5);
frame.remove(pan6);
frame.setLayout(new GridLayout(2,1));
title.setIcon(new ImageIcon ("SocialNetworkingLiveLabel.jpg"));
signButH.setIcon(new ImageIcon("signUp.jpg"));
pan1.removeAll();
pan1.setLayout(new GridLayout(1,1));
pan2.removeAll();
pan2.setLayout(new GridLayout(1,2));
pan1.add(title);
pan2.add(loginButH);
pan2.add(signButH);
frame.add(pan1);
frame.add(pan2);
frame.pack();
frame.setSize(frame.getPreferredSize());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void displayLoginPage()
{
frame.remove(pan4);
frame.remove(pan5);
frame.remove(pan6);
frame.setLayout(new GridLayout(3,1));
title = new JLabel (new ImageIcon ("SocialNetworkingLiveLabel.jpg"));
pan1.removeAll();
pan1.setLayout(new GridLayout(1,1));
pan2.removeAll();
pan2.setLayout(new GridLayout(2,2));
pan3.removeAll();
pan3.setLayout(new GridLayout(1,2));
userL = new JLabel (new ImageIcon ("UserL.jpg"));
passL = new JLabel (new ImageIcon ("PassL.jpg"));
backBut.setIcon(new ImageIcon("BACKLOG.jpg"));
pan1.add(title);
pan2.add(userL);
pan2.add(userF);
pan2.add(passL);
pan2.add(passF);
pan3.add(backBut);
pan3.add(loginButL);
frame.add(pan1);
frame.add(pan2);
frame.add(pan3);
frame.pack();
frame.setSize(frame.getPreferredSize());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public String getUsername3()
{
String usernameS = userF.getText();
return usernameS;
}
public String getPassword3()
{
String password = String.valueOf(passF.getPassword());
return password;
}
public void actionPerformed(ActionEvent event)
{
InterfaceWork iwObj = new InterfaceWork();
if(event.getSource() == loginButH)
{
displayLoginPage();
}
if(event.getSource() == signButH)
{
displaySignUpPage();
}
if(event.getSource() == backBut)
{
displayHomePage();
}
if(event.getSource() == loginButL)
{
try
{
System.out.println(getUsername3());
iwObj.login();
System.out.println(getUsername3());
}
catch(IOException ioe)
{
}
}
}
And the second class (Nothing left out):
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class InterfaceWork
{
private Work[] usersAr = new Work [1];
public InterfaceWork()
{
}
public void getArray() throws IOException
{
Scanner sc = new Scanner (new File("profiles.txt"));
int count = 0;
while (sc.hasNextLine())
{
count++;
sc.nextLine();
}
sc.close ();
usersAr = new Work [count];
sc = new Scanner (new File("profiles.txt"));
Scanner s;
for (int i = 0 ; i < usersAr.length ; i++)
{
s = new Scanner(sc.nextLine()).useDelimiter("#");
usersAr [i] = new Work (s.next(),s.next(),s.next(),s.next(),s.nextInt(),s.next(),s.nextBoolean(),(i+1));
s.close();
}
sc.close ();
}
public void login() throws IOException
{
Interface iObj = new Interface();
int countLog = 0;
System.out.println("Login");
try
{
System.out.println("try");
String username = iObj.getUsername3();
System.out.println(username);
}
catch(NullPointerException npe)
{
System.out.println("npe");
}
/*
while!((iObj.userF).equalsIgnoreCase(getUsername2())
{
countLog++;
}
if((iObj.passF).equalsIgnoreCase(usersAr[countLog].getPassword()))
{
displayProfileHomePage();
}
else if((iObj.passF).equalsIgnoreCase("Piet"))
{
displayWrongPasswordFrame();
}
else
{
displayUserNotRegFrame();
} */
}
public String getUsername2() throws IOException
{
getArray();
return usersAr[0].getUsername();
}
public String getPassword2() throws IOException
{
getArray();
return usersAr[0].getPassword();
}
public String getName2() throws IOException
{
getArray();
return usersAr[0].getName();
}
public String getSurname2() throws IOException
{
getArray();
return usersAr[0].getSurname();
}
public int getAge2() throws IOException
{
getArray();
return usersAr[0].getAge();
}
public String getNationality2() throws IOException
{
getArray();
return usersAr[0].getNationality();
}
public String getGender2() throws IOException
{
getArray();
return usersAr[0].getGender();
}
public int getUserNo2() throws IOException
{
getArray();
return usersAr[0].getUserNo();
}
}