Solved

IF / ELSE: Changing Variable Values

Posted on 2001-06-11
3
168 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
print map entry 34 72
configure dependency in POM for new database 3 34
ejb on wildfly 5 30
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

860 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