Solved

IF / ELSE: Changing Variable Values

Posted on 2001-06-11
3
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 45
ejb on wildfly 5 46
jmss example java 2 33
Why is enum singleton a better approach than static factory 3 51
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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