Solved

IF / ELSE: Changing Variable Values

Posted on 2001-06-11
3
160 Views
Last Modified: 2012-05-04
Hello,
I'm getting NullPointerExceptions when i compile my program and I think I'm doing something wrong. So that I don't confuse you I'll give you the code before the exceptions began to happen and tell you what I need it to do. Here's a basic overview of the code: (much of the code has been excluded to keep things simple) (there's an explanation after the code)

class MailGUI extends JFrame
{
JCheckBox altserverCheckBox = new JCheckBox("Use an Alternate Server and/or port");

altserverCheckBox.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        altsvrBox_actionPerformed(e);
      }
    });

void altsvrBox_actionPerformed(ActionEvent e)
{
serverTextField.setEnabled(altserverCheckBox.isSelected());
portTextField.setEnabled(altserverCheckBox.isSelected());
    if(altserverCheckBox.isSelected()){
    serverTextField.setBackground(bgwhite);
    portTextField.setBackground(bgwhite);}
    else{
    serverTextField.setBackground(bggrey);
    portTextField.setBackground(bggrey);}
}

}

class Message {
  String destination;

  public void setDestination(String dest) {
    destination = dest;
  }
  public String getDestination() {
    return destination;
  }
  public String getDestinationHost() {
    return destination.substring(destination.indexOf('@')+1);
  }
}

class MailTalk {
Message msg;

String destination = msg.getDestinationHost();
}


I want it to work so that if the checkbox defined in MailGui is checked it sets "destination" (String defined in MailTalk) to the text from a text field on the same GUI. If the box isn't checked I want it to be set to what it's currently bieng set to.

If this is confusing, don't be detured, just ask for clarification. Thanks.
0
Comment
Question by:m_morgan
3 Comments
 
LVL 4

Accepted Solution

by:
ykf2000 earned 50 total points
ID: 6180014
try modifying to this:

public String getDestinationHost() {
   return destination.equals("") ? "" : destination.substring(destination.indexOf('@')+1);
 }
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6180370
where do you declare portTextField and serverTextField ? I cannot see'it anyware. If you don't declare'it you should do'it. Any way, post the full exception
0
 

Author Comment

by:m_morgan
ID: 6183254
Ovi: the exception is not the issue it's what to do instead. I did declare the two text fields as well as a bunch of other stuff that I left out.

ykf2000: that code compiles but when the function is called the program does nothing. Was I supposed to substitute certian things for names of parts of my program? Could you give me the code if I wanted it to decide where to go based on whether the: altserverCheckBox is checked. If it isn't checked I want it to return this: destination.substring(destination.indexOf('@')+1). If it is checked I want it to return the value of whatever is in the serverTextField.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now