3 tier architecture causing problems
Hello
We are having some problems with our code after converting our code from just one form to the 3 tier architecture
The problem seems to be between our gui and logic layer.
When we run our program we get a nullpointer exception, i will just link the code and exception.
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Logic.doesUserExist(Logic.java:43)
at GUI.jButton4ActionPerformed(GUI.java:643)
at GUI.access$000(GUI.java:2)
at GUI$1.actionPerformed(GUI.java:124)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
This is our code for our gui where i have cut out the constructor, some used get methods and the button we press
Code:
public class GUI extends javax.swing.JFrame {
public Logic l;
public GUI() {
initComponents();
jDialog1.setVisible(true);
l = new Logic();
}
public String getUser(){
return jTextField5.getText();
}
public char[] getPass(){
return jPasswordField1.getPassword();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
if(l.doesUserExist() == true ){
if(l.doesPatientExist() == true ){
setVisible(false);
jLabel8.setText("Patient do not exist.");
jDialog3.setVisible(true);
}
else{
jTextField7.setText(l.getdName() + ", " + l.getdCPR() + ".");
jTextField11.setText(l.getdName());
jTextField2.setText(l.getdCPR());
jTextField9.setText(l.getdAdd());
jTextField3.setText(Integer.toString(l.getdTelnr()));
jTextField6.setText(l.getdComment());
}
}
else{
jDialog3.setVisible(true);
jLabel8.setText("Wrong password/username.");
}
}
}
This is our logic class where i have cut out the constructor and the method we call in the gui layer.
Code:
public class Logic {
private GUI g;
private Data d;
public Logic(){
d = new Data();
}
public boolean doesUserExist(){
if(g.getUser().equals("Lęge"))
{
String a = String.valueOf(g.getPass());
if(a.equals("1234"))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
From what i can see is the fault is at line 43 at the if sentence in the logic, but i cant figure out what the exception is.
thank you
Zezinz
Re: 3 tier architecture causing problems
Quote:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Logic.doesUserExist(Logic.java:43)
There is a null variable on line 43 in Logic.java. Look at that line of code and find the null variable. Then backtrack to see why that variable does not have a valid value.
If you can not see what variable is null, add a println just before line 43 to print out the values of all the variables used on line 43.
Re: 3 tier architecture causing problems
Quote:
Originally Posted by
zezinz
Hello
Hello and welcome to Java-Forums.org!
I have taken the liberty of adding code tags to your post. If you edit it, you'll see how I did it.
Quote:
This is our logic class where i have cut out the constructor and the method we call in the gui layer. Code:
public class Logic {
private GUI g;
private Data d;
public Logic(){
d = new Data();
}
public boolean doesUserExist(){
if(g.getUser().equals("Lęge"))
{
String a = String.valueOf(g.getPass());
if(a.equals("1234"))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
From what i can see is the fault is at line 43 at the if sentence in the logic, but i cant figure out what the exception is.
What I can't see is where you've set your GUI variable, g, to refer to a valid GUI object. Usually this would require giving the Logic class a mutator or setter method, perhaps one called setGui(GUI g) and calling it from the controller or from whatever class starts the whole program and creates and displays GUI.
Re: 3 tier architecture causing problems
thank you for your help.
we found out that we needed to send a parameter with the method to the logic class.
so it is now fixed
Zezinz
Re: 3 tier architecture causing problems
Quote:
Originally Posted by
zezinz
thank you for your help.
we found out that we needed to send a parameter with the method to the logic class.
so it is now fixed
I'm glad that it's fixed, but I'm confused by what you mean by "a parameter with 'the method'"? Are you sure that the parameter isn't a GUI object either in a setter method as I mentioned above or in the constructor?
Re: 3 tier architecture causing problems
we changed our code to
Code:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
if(l.doesUserExist(getUser(), getPass()) == true ){ //added getUser() and getPass()
if(l.doesPatientExist(getCPR()) == true ){ //add getCPR()
setVisible(false);
jLabel8.setText("Patient do not exist.");
jDialog3.setVisible(true);
}
else{
jTextField7.setText(l.getdName() + ", " + l.getdCPR() + ".");
jTextField11.setText(l.getdName());
jTextField2.setText(l.getdCPR());
jTextField9.setText(l.getdAdd());
jTextField3.setText(Integer.toString(l.getdTelnr()));
jTextField6.setText(l.getdComment());
setVisible(true);
jDialog1.dispose();
l.getBlobFromDatabase(l.getdCPR(), l.getdName());
}
}
else{
jDialog3.setVisible(true);
jLabel8.setText("Wrong password/username.");
}
}
By parameters we mean the bold methods so we can transfer some variables to the other class.
Code:
public boolean doesUserExist(String user, char[] pass){ //added String user and char[] pass
if(user.equals("Lęge")) //added user
{
String a = String.valueOf(pass); // added pass
if(a.equals("1234"))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
then we use those parameters here in the other class, instead of calling the methods across the class.
Re: 3 tier architecture causing problems
OK, got it.
As an aside, please don't try to "bold" or italicize posted code. Instead use code tags so your code is readable. Please edit your code above to see how I have added code tags to your post.
Re: 3 tier architecture causing problems
yes i made it bold at first because i couldnt figure out how to use code tags :) but i found out.