Calling another class method using a button

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 11589 Views - Last Post: 01 September 2010 - 03:33 AM Rate Topic: -----

#1 soldierno77   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 30-August 10

Calling another class method using a button

Posted 30 August 2010 - 05:51 AM

I have a Member class with check() method
public class Member extends JFrame implements ActionListener
{
    ......
	public boolean check(String id, String pass)
	{
		if(id == memberID && pass == password)
		return true;
		else
		return false;
	}
    ......
}


Then I need to call that check() method from my MainMenu class using button b1...
public class MainMenu extends JFrame implements ActionListener
{
    ......
    public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == b2)
		{
			memlist[i] = new Member();
			memlist[i].setSize(400,200);
			memlist[i].setVisible(true);
			i++;
		}
		if(e.getSource() == b1)
		{
			int temp = 0;
			for(int j = 0; j < i; j++)
			{
				if(memlist[i].check(t1.getText(), t2.getText()) == true);
				{
					JOptionPane.showMessageDialog(null, "You are logged in");
					temp++;
				}
			}
			if(temp == 0)
			{
				JOptionPane.showMessageDialog(null, "Wrong ID an password!!!");
			}
		}
	}
    ....
}


I would get an exception every time I clicked b1. How can I call the method() without getting exception?

This post has been edited by soldierno77: 30 August 2010 - 05:52 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Calling another class method using a button

#2 b0ng01   User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Calling another class method using a button

Posted 30 August 2010 - 06:11 AM

Need the exception and your full code.
Was This Post Helpful? 0
  • +
  • -

#3 soldierno77   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 30-August 10

Re: Calling another class method using a button

Posted 30 August 2010 - 06:19 AM

Here is the full code for Member class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class Member extends JFrame implements ActionListener
{
	String memberID, name, password, memType, phone, email;
	Label l1 = new Label("ID");
	Label l2 = new Label("Name");
	Label l3 = new Label("Password");
	Label l4 = new Label("Member Type");
	Label l5 = new Label("Phone");
	Label l6 = new Label("Email");
	TextField t1 = new TextField(null, 10);
	TextField t2 = new TextField(null, 50);
	TextField t3 = new TextField(null, 12);
	TextField t4 = new TextField(null, 10);
	TextField t5 = new TextField(null, 50);
	Choice c = new Choice();
	Button b1 = new Button("OK");
	Button b2 = new Button("Reset");

	public Member()
	{
		setLayout(new GridLayout(7,2));
		c.add("Staff");
		c.add("Student");
		t3.setEchoChar('*');
		add(l1);add(t1);add(l3);add(t3);add(l2);add(t2);add(l4);add(c);add(l5);add(t4);add(l6);add(t5);add(b1);add(b2);
		b1.addActionListener(this);
		b2.addActionListener(this);
	}

	public void display()
	{
		if(memberID.equals("") || name.equals("") || password.equals("") || phone.equals("") || email.equals(""))
		JOptionPane.showMessageDialog(null, "Please complete the form!!!");
		else
		{
			JOptionPane.showMessageDialog(null, "ID\t: " + memberID + "\n" + "Password\t: " + password + "\n" + "Name\t: " + name + "\n" + "Member Type\t: " + memType + "\n" + "Phone\t: " + phone + "\n" + "Email\t: " + email + "\nUser has been registered, please close the window.");
		}
	}
	
	public boolean check(String id, String pass)
	{
		if(id == memberID && pass == password)
		return true;
		else
		return false;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == b1)
		{
			memberID = t1.getText();
			name = t2.getText();
			password = t3.getText();
			phone = t4.getText();
			email = t5.getText();
			memType = c.getSelectedItem();
			display();
		}
		if(e.getSource() == b2)
		{
			t1.setText(null);
			t2.setText(null);
			t3.setText(null);
			t4.setText(null);
			t5.setText(null);
		}
	}
}


And here is the MainMenu class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class MainMenu extends JFrame implements ActionListener
{
	Label l1 = new Label("User ID");
	Label l2 = new Label("Password");
	TextField t1 = new TextField(10);
	TextField t2 = new TextField(12);
	Button b1 = new Button("Log In");
	Button b2 = new Button("Register");
	Member[] memlist = new Member[200];
	int i = 0;
	
	public MainMenu()
	{
		setLayout(new GridLayout(3,2));
		t2.setEchoChar('*');
		add(l1);add(t1);add(l2);add(t2);add(b1);add(b2);
		b1.addActionListener(this);
		b2.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == b2)
		{
			memlist[i] = new Member();
			memlist[i].setSize(400,200);
			memlist[i].setVisible(true);
			i++;
		}
		if(e.getSource() == b1)
		{
			int temp = 0;
			for(int j = 0; j < i; j++)
			{
				if(memlist[i].check(t1.getText(), t2.getText()) == true);
				{
					JOptionPane.showMessageDialog(null, "You are logged in");
					temp++;
				}
			}
			if(temp == 0)
			{
				JOptionPane.showMessageDialog(null, "Wrong ID and password!!!");
			}
		}
	}
	
	public static void main(String[] args)
	{
		JFrame frame = new MainMenu();
		frame.setSize(400, 120);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}


Here is the exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MainMenu.actionPerformed(MainMenu.java:40)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Was This Post Helpful? 0
  • +
  • -

#4 b0ng01   User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Calling another class method using a button

Posted 30 August 2010 - 06:33 AM

String memberID, name, password, memType, phone, email;

You never instantiate any of those.

But in display you are using .equals() on them.
Was This Post Helpful? 0
  • +
  • -

#5 soldierno77   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 30-August 10

Re: Calling another class method using a button

Posted 30 August 2010 - 06:41 AM

View Postb0ng01, on 30 August 2010 - 05:33 AM, said:

String memberID, name, password, memType, phone, email;

You never instantiate any of those.

But in display you are using .equals() on them.


I only use the equals on them after I get the data from the user input and the user click the b1 button of Member class. But I don't think that's the problem CMIIW.
Was This Post Helpful? 0
  • +
  • -

#6 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Calling another class method using a button

Posted 30 August 2010 - 07:13 AM

I think your problem is here:

 if(memlist[i].check(t1.getText(), t2.getText()) == true);



You should not have the variable 'i' as the index of memlist. Instead of it you should have the variable 'j' which is the variable that is incrementing its value.

Another thing to point out is that you should not use '==' for compare two strings:
 if(id == memberID && pass == password)



Because this is for compare memory addres instead of values. You must use
the equals method of the String class.
Was This Post Helpful? 1
  • +
  • -

#7 soldierno77   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 30-August 10

Re: Calling another class method using a button

Posted 30 August 2010 - 07:17 AM

View Postguidojavier, on 30 August 2010 - 06:13 AM, said:

I think your problem is here:

 if(memlist[i].check(t1.getText(), t2.getText()) == true);



You should not have the variable 'i' as the index of memlist. Instead of it you should have the variable 'j' which is the variable that is incrementing its value.

Another thing to point out is that you should not use '==' for compare two strings:
 if(id == memberID && pass == password)



Because this is for compare memory addres instead of values. You must use
the equals method of the String class.


It works. Thanks. Plus rep for you.
Was This Post Helpful? 0
  • +
  • -

#8 b0ng01   User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Calling another class method using a button

Posted 30 August 2010 - 07:19 AM

for(int j = 0; j < i; j++)
			{
				if(memlist[i].check(t1.getText(), t2.getText()) == true);
				{
					JOptionPane.showMessageDialog(null, "You are logged in");
					temp++;
				}
			}



Everything is correct except that, you try to access one position past where your array has data.

if(memlist[j].check(t1.getText(), t2.getText()) == true);



I just compiled the code, it works with that change.
Was This Post Helpful? 0
  • +
  • -

#9 ipushmycar   User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Calling another class method using a button

Posted 30 August 2010 - 02:30 PM

			
if(temp == 0)
			{
				JOptionPane.showMessageDialog(null, "Wrong ID an password!!!");
			}
		}
	}
    ....
}


Just want to point out I think you mean Wrong ID AND password not an.

This post has been edited by ipushmycar: 30 August 2010 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Calling another class method using a button

Posted 30 August 2010 - 05:37 PM

This is obviously wrong

	public boolean check(String id, String pass)
	{
		if(id == memberID && pass == password)
		return true;
		else
		return false;
	}


You cannot compare 2 String using the == operator
you should do
	public boolean check(String id, String pass)
	{
		return (id == memberID && pass.equals(password));
	}


Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Calling another class method using a button

Posted 30 August 2010 - 06:08 PM

pbl, this code returns true:

public class Main {
	
	private String memberID = "dog";
	private String password = "pass";
	
	public boolean check(String id, String pass)
	{
		if(id == memberID && pass == password)
		return true;
		else
		return false;
	}


    public static void main(String args[])
	{
	 	Main main = new Main();
	 	System.out.println(main.check("dog", "pass"));
	}
}



Because it's using literals. However, if you use a String constructor, it will be false, so you can compare it using the intern() methods to make it always true (assuming that memberID and pass are String literals).

public class Main {
	
	private String memberID = "dog";
	private String password = "pass";
	
	public boolean check(String id, String pass)
	{
		if(id.intern() == memberID && pass.intern() == password)
		return true;
		else
		return false;
	}


    public static void main(String args[])
	{
	 	Main main = new Main();
	 	System.out.println(main.check(new String("dog"), "pass"));
	}
}



This returns true also...

Just thought I'd point out that application of intern().
Was This Post Helpful? 0
  • +
  • -

#12 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: Calling another class method using a button

Posted 30 August 2010 - 06:52 PM

just adding to this but i rarely use .equals tnx to this post i learned something +rep to the topic creator XD
Was This Post Helpful? 0
  • +
  • -

#13 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Calling another class method using a button

Posted 30 August 2010 - 07:09 PM

Dogstopper, doesnt intern explictly rely on the .equals method though unless I am tottally on the wrong tracks here? Its just that the api states

Quote

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Calling another class method using a button

Posted 30 August 2010 - 07:12 PM

View Postnick2price, on 30 August 2010 - 09:09 PM, said:

Dogstopper, doesnt intern explictly rely on the .equals method though unless I am tottally on the wrong tracks here? Its just that the api states

Quote

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.


Sort of...I mean, that's the result of intern(). I wrote a tutorial on how it worked here:
http://www.dreaminco...he-string-pool/
Was This Post Helpful? 1
  • +
  • -

#15 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Calling another class method using a button

Posted 30 August 2010 - 07:32 PM

Very interesting read. I suppose everyone is always taught the .equals method because there surely cant be any advantage of comparing literal Strings using == unless you have a substantial program. I also suppose that you can never go wrong with .equals. Was very interested in the String pool though, never knew about that, will have to read up on it more along with the internationialisation topic i noticed when looking at reflection on sun.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2