Java - random “null” popping up during a Try/Catch












-2















I have a script that tests the validity of characters entered in order to learn Try/Catch. The numbers must be entered in a strict "$123.45" format or else the Try/Catch prints an error. It needs to be able to catch all integer errors and print them. Currently, if a dollar AND a cent are entered incorrectly the script works fine and prints both errors. However, if ONLY an error is found when entering cents, the print line says begins with "null".



Example (working):
Input:

Please enter amount of sale in form $#.## ("q" to quit): $12d.de



Print:
Invalid dollar format - For input string: "12d"



Invalid cents format - For input string: "de"



Example (NOT working):
Input:

Please enter amount of sale in form $#.## ("q" to quit): $123.de



Print:
nullInvalid cents format - For input string: "de"



The error is in the "+=" in the Catch for myCent:



"myBad += "Invalid cents format - For input string: "" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + ""n";"



How do I get the Catch to not print "null" without losing the multi-error print functionality? Any and all help us GREATLY appreciated.



This program consists of the below two scripts, the "DKSaleCheck.java" is my problem child:
DKUnit6Ch15.java



import java.util.*; //Load all Utility Classes

public class DKUnit6Ch15 { //Begin Class DKUnit6Ch15

public static void main(String args) { //Begin Main
Scanner myScan = new Scanner(System.in); //Initialize the Scanner
String myAmount; //Define a new Variable

while(true) { //Begin infinite While Loop
System.out.print("Please enter amount of sale in form $#.## ("q" to quit): "); //Print the text
myAmount = myScan.next(); //Define a new Variable with the next user input

if(myAmount.equalsIgnoreCase("q")) { //Begin If Statement (if the user entered "q" then do the following...)
break; //Break the script
} //End If Statement

DKSaleCheck myCash = new DKSaleCheck(myAmount); //Define a new Variable and send it to the Constructor
myCash.print(); //Print the Output from the Constructor
} //End infinite While Loop

myScan.close(); //Close the Scanner

} //End Main

} //End Class DKUnit6Ch15


DKSaleCheck.java



    class DKSaleCheck{ //Begin Class DKSaleCheck

int myDollar; //Define a new Variable
int myCent; //Define a new Variable
String myBad; //Define a new String Variable

public DKSaleCheck(String mySale) { //Begin Method DKSaleCheck and receive sale as a string
myBad = null; //Define a new Variable

if(!mySale.startsWith("$")) { //Begin If Statement (if mySale does NOT start with a "$")
myBad = "Invalid sale format missing "$" - " + mySale + "n"; //Fill the Variable with the String data
} //End If Statement

else if(mySale.indexOf('.') == -1) { //Begin ElseIf Statement (if mySale does NOT contain a ".")
myBad = "Invalid sale format missing "." - " + mySale + "n"; //Fill the Variable with the String data
} //End ElseIf Statement

else{ //Begin Else Statement
try{ //Begin Try Statement
myDollar = Integer.parseInt(mySale.substring(1, mySale.indexOf('.'))); //Fill the Variable with the data if ONLY integers are detected from Index 1 to the "."
} //End Try Statement

catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad = "Invalid dollar format - For input string: "" + mySale.substring(1,mySale.indexOf('.')) + ""n"; //Fill the Variable with the String data
} //End Catch Statement

try{ //Begin Try Statement
myCent = Integer.parseInt(mySale.substring(mySale.indexOf('.') + 1,mySale.length())); //Fill the Variable with the data if ONLY integers are detected after the "."
} //End Try Statement
catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad += "Invalid cents format - For input string: "" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + ""n"; //Fill the Variable with the String data
} //End Catch Statement
} //End Else Statement

} //End Method DKSaleCheck

public void print(){ //Begin Print Method

if(myBad != null){ //Begin If Statement (if the error variable is NOT null)
System.out.println(myBad); //Print the String Variable
} //End If Statement

else{ //Begin Else Statement
System.out.println("$" + myDollar + "." + myCent); //Print the text
System.out.println(myDollar + " dollars and " + myCent + " centsn"); //Print the text
} //End Else Statement

} //End Print Method

} //End Class DKSaleCheck









share|improve this question

























  • You have myBad += where you need myBad =. As it is initially null, you get a null.

    – user207421
    Nov 21 '18 at 23:41











  • But if I remove the "+" it will no longer find multiples. It ends after printing only the error with cents. For instance if I enter "$12d.de" it only shows the error in the cents part and not the dollar part.

    – Dillon
    Nov 21 '18 at 23:43













  • Please, format the code. On debug you can see what exactly is null in your case.

    – Sergei Sirik
    Nov 21 '18 at 23:44






  • 1





    One solution is to change myBad = null; to myBad = "";

    – Scary Wombat
    Nov 22 '18 at 0:17











  • What multiples? You can only get one exception at a time.

    – user207421
    Nov 22 '18 at 9:15


















-2















I have a script that tests the validity of characters entered in order to learn Try/Catch. The numbers must be entered in a strict "$123.45" format or else the Try/Catch prints an error. It needs to be able to catch all integer errors and print them. Currently, if a dollar AND a cent are entered incorrectly the script works fine and prints both errors. However, if ONLY an error is found when entering cents, the print line says begins with "null".



Example (working):
Input:

Please enter amount of sale in form $#.## ("q" to quit): $12d.de



Print:
Invalid dollar format - For input string: "12d"



Invalid cents format - For input string: "de"



Example (NOT working):
Input:

Please enter amount of sale in form $#.## ("q" to quit): $123.de



Print:
nullInvalid cents format - For input string: "de"



The error is in the "+=" in the Catch for myCent:



"myBad += "Invalid cents format - For input string: "" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + ""n";"



How do I get the Catch to not print "null" without losing the multi-error print functionality? Any and all help us GREATLY appreciated.



This program consists of the below two scripts, the "DKSaleCheck.java" is my problem child:
DKUnit6Ch15.java



import java.util.*; //Load all Utility Classes

public class DKUnit6Ch15 { //Begin Class DKUnit6Ch15

public static void main(String args) { //Begin Main
Scanner myScan = new Scanner(System.in); //Initialize the Scanner
String myAmount; //Define a new Variable

while(true) { //Begin infinite While Loop
System.out.print("Please enter amount of sale in form $#.## ("q" to quit): "); //Print the text
myAmount = myScan.next(); //Define a new Variable with the next user input

if(myAmount.equalsIgnoreCase("q")) { //Begin If Statement (if the user entered "q" then do the following...)
break; //Break the script
} //End If Statement

DKSaleCheck myCash = new DKSaleCheck(myAmount); //Define a new Variable and send it to the Constructor
myCash.print(); //Print the Output from the Constructor
} //End infinite While Loop

myScan.close(); //Close the Scanner

} //End Main

} //End Class DKUnit6Ch15


DKSaleCheck.java



    class DKSaleCheck{ //Begin Class DKSaleCheck

int myDollar; //Define a new Variable
int myCent; //Define a new Variable
String myBad; //Define a new String Variable

public DKSaleCheck(String mySale) { //Begin Method DKSaleCheck and receive sale as a string
myBad = null; //Define a new Variable

if(!mySale.startsWith("$")) { //Begin If Statement (if mySale does NOT start with a "$")
myBad = "Invalid sale format missing "$" - " + mySale + "n"; //Fill the Variable with the String data
} //End If Statement

else if(mySale.indexOf('.') == -1) { //Begin ElseIf Statement (if mySale does NOT contain a ".")
myBad = "Invalid sale format missing "." - " + mySale + "n"; //Fill the Variable with the String data
} //End ElseIf Statement

else{ //Begin Else Statement
try{ //Begin Try Statement
myDollar = Integer.parseInt(mySale.substring(1, mySale.indexOf('.'))); //Fill the Variable with the data if ONLY integers are detected from Index 1 to the "."
} //End Try Statement

catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad = "Invalid dollar format - For input string: "" + mySale.substring(1,mySale.indexOf('.')) + ""n"; //Fill the Variable with the String data
} //End Catch Statement

try{ //Begin Try Statement
myCent = Integer.parseInt(mySale.substring(mySale.indexOf('.') + 1,mySale.length())); //Fill the Variable with the data if ONLY integers are detected after the "."
} //End Try Statement
catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad += "Invalid cents format - For input string: "" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + ""n"; //Fill the Variable with the String data
} //End Catch Statement
} //End Else Statement

} //End Method DKSaleCheck

public void print(){ //Begin Print Method

if(myBad != null){ //Begin If Statement (if the error variable is NOT null)
System.out.println(myBad); //Print the String Variable
} //End If Statement

else{ //Begin Else Statement
System.out.println("$" + myDollar + "." + myCent); //Print the text
System.out.println(myDollar + " dollars and " + myCent + " centsn"); //Print the text
} //End Else Statement

} //End Print Method

} //End Class DKSaleCheck









share|improve this question

























  • You have myBad += where you need myBad =. As it is initially null, you get a null.

    – user207421
    Nov 21 '18 at 23:41











  • But if I remove the "+" it will no longer find multiples. It ends after printing only the error with cents. For instance if I enter "$12d.de" it only shows the error in the cents part and not the dollar part.

    – Dillon
    Nov 21 '18 at 23:43













  • Please, format the code. On debug you can see what exactly is null in your case.

    – Sergei Sirik
    Nov 21 '18 at 23:44






  • 1





    One solution is to change myBad = null; to myBad = "";

    – Scary Wombat
    Nov 22 '18 at 0:17











  • What multiples? You can only get one exception at a time.

    – user207421
    Nov 22 '18 at 9:15
















-2












-2








-2








I have a script that tests the validity of characters entered in order to learn Try/Catch. The numbers must be entered in a strict "$123.45" format or else the Try/Catch prints an error. It needs to be able to catch all integer errors and print them. Currently, if a dollar AND a cent are entered incorrectly the script works fine and prints both errors. However, if ONLY an error is found when entering cents, the print line says begins with "null".



Example (working):
Input:

Please enter amount of sale in form $#.## ("q" to quit): $12d.de



Print:
Invalid dollar format - For input string: "12d"



Invalid cents format - For input string: "de"



Example (NOT working):
Input:

Please enter amount of sale in form $#.## ("q" to quit): $123.de



Print:
nullInvalid cents format - For input string: "de"



The error is in the "+=" in the Catch for myCent:



"myBad += "Invalid cents format - For input string: "" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + ""n";"



How do I get the Catch to not print "null" without losing the multi-error print functionality? Any and all help us GREATLY appreciated.



This program consists of the below two scripts, the "DKSaleCheck.java" is my problem child:
DKUnit6Ch15.java



import java.util.*; //Load all Utility Classes

public class DKUnit6Ch15 { //Begin Class DKUnit6Ch15

public static void main(String args) { //Begin Main
Scanner myScan = new Scanner(System.in); //Initialize the Scanner
String myAmount; //Define a new Variable

while(true) { //Begin infinite While Loop
System.out.print("Please enter amount of sale in form $#.## ("q" to quit): "); //Print the text
myAmount = myScan.next(); //Define a new Variable with the next user input

if(myAmount.equalsIgnoreCase("q")) { //Begin If Statement (if the user entered "q" then do the following...)
break; //Break the script
} //End If Statement

DKSaleCheck myCash = new DKSaleCheck(myAmount); //Define a new Variable and send it to the Constructor
myCash.print(); //Print the Output from the Constructor
} //End infinite While Loop

myScan.close(); //Close the Scanner

} //End Main

} //End Class DKUnit6Ch15


DKSaleCheck.java



    class DKSaleCheck{ //Begin Class DKSaleCheck

int myDollar; //Define a new Variable
int myCent; //Define a new Variable
String myBad; //Define a new String Variable

public DKSaleCheck(String mySale) { //Begin Method DKSaleCheck and receive sale as a string
myBad = null; //Define a new Variable

if(!mySale.startsWith("$")) { //Begin If Statement (if mySale does NOT start with a "$")
myBad = "Invalid sale format missing "$" - " + mySale + "n"; //Fill the Variable with the String data
} //End If Statement

else if(mySale.indexOf('.') == -1) { //Begin ElseIf Statement (if mySale does NOT contain a ".")
myBad = "Invalid sale format missing "." - " + mySale + "n"; //Fill the Variable with the String data
} //End ElseIf Statement

else{ //Begin Else Statement
try{ //Begin Try Statement
myDollar = Integer.parseInt(mySale.substring(1, mySale.indexOf('.'))); //Fill the Variable with the data if ONLY integers are detected from Index 1 to the "."
} //End Try Statement

catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad = "Invalid dollar format - For input string: "" + mySale.substring(1,mySale.indexOf('.')) + ""n"; //Fill the Variable with the String data
} //End Catch Statement

try{ //Begin Try Statement
myCent = Integer.parseInt(mySale.substring(mySale.indexOf('.') + 1,mySale.length())); //Fill the Variable with the data if ONLY integers are detected after the "."
} //End Try Statement
catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad += "Invalid cents format - For input string: "" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + ""n"; //Fill the Variable with the String data
} //End Catch Statement
} //End Else Statement

} //End Method DKSaleCheck

public void print(){ //Begin Print Method

if(myBad != null){ //Begin If Statement (if the error variable is NOT null)
System.out.println(myBad); //Print the String Variable
} //End If Statement

else{ //Begin Else Statement
System.out.println("$" + myDollar + "." + myCent); //Print the text
System.out.println(myDollar + " dollars and " + myCent + " centsn"); //Print the text
} //End Else Statement

} //End Print Method

} //End Class DKSaleCheck









share|improve this question
















I have a script that tests the validity of characters entered in order to learn Try/Catch. The numbers must be entered in a strict "$123.45" format or else the Try/Catch prints an error. It needs to be able to catch all integer errors and print them. Currently, if a dollar AND a cent are entered incorrectly the script works fine and prints both errors. However, if ONLY an error is found when entering cents, the print line says begins with "null".



Example (working):
Input:

Please enter amount of sale in form $#.## ("q" to quit): $12d.de



Print:
Invalid dollar format - For input string: "12d"



Invalid cents format - For input string: "de"



Example (NOT working):
Input:

Please enter amount of sale in form $#.## ("q" to quit): $123.de



Print:
nullInvalid cents format - For input string: "de"



The error is in the "+=" in the Catch for myCent:



"myBad += "Invalid cents format - For input string: "" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + ""n";"



How do I get the Catch to not print "null" without losing the multi-error print functionality? Any and all help us GREATLY appreciated.



This program consists of the below two scripts, the "DKSaleCheck.java" is my problem child:
DKUnit6Ch15.java



import java.util.*; //Load all Utility Classes

public class DKUnit6Ch15 { //Begin Class DKUnit6Ch15

public static void main(String args) { //Begin Main
Scanner myScan = new Scanner(System.in); //Initialize the Scanner
String myAmount; //Define a new Variable

while(true) { //Begin infinite While Loop
System.out.print("Please enter amount of sale in form $#.## ("q" to quit): "); //Print the text
myAmount = myScan.next(); //Define a new Variable with the next user input

if(myAmount.equalsIgnoreCase("q")) { //Begin If Statement (if the user entered "q" then do the following...)
break; //Break the script
} //End If Statement

DKSaleCheck myCash = new DKSaleCheck(myAmount); //Define a new Variable and send it to the Constructor
myCash.print(); //Print the Output from the Constructor
} //End infinite While Loop

myScan.close(); //Close the Scanner

} //End Main

} //End Class DKUnit6Ch15


DKSaleCheck.java



    class DKSaleCheck{ //Begin Class DKSaleCheck

int myDollar; //Define a new Variable
int myCent; //Define a new Variable
String myBad; //Define a new String Variable

public DKSaleCheck(String mySale) { //Begin Method DKSaleCheck and receive sale as a string
myBad = null; //Define a new Variable

if(!mySale.startsWith("$")) { //Begin If Statement (if mySale does NOT start with a "$")
myBad = "Invalid sale format missing "$" - " + mySale + "n"; //Fill the Variable with the String data
} //End If Statement

else if(mySale.indexOf('.') == -1) { //Begin ElseIf Statement (if mySale does NOT contain a ".")
myBad = "Invalid sale format missing "." - " + mySale + "n"; //Fill the Variable with the String data
} //End ElseIf Statement

else{ //Begin Else Statement
try{ //Begin Try Statement
myDollar = Integer.parseInt(mySale.substring(1, mySale.indexOf('.'))); //Fill the Variable with the data if ONLY integers are detected from Index 1 to the "."
} //End Try Statement

catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad = "Invalid dollar format - For input string: "" + mySale.substring(1,mySale.indexOf('.')) + ""n"; //Fill the Variable with the String data
} //End Catch Statement

try{ //Begin Try Statement
myCent = Integer.parseInt(mySale.substring(mySale.indexOf('.') + 1,mySale.length())); //Fill the Variable with the data if ONLY integers are detected after the "."
} //End Try Statement
catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad += "Invalid cents format - For input string: "" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + ""n"; //Fill the Variable with the String data
} //End Catch Statement
} //End Else Statement

} //End Method DKSaleCheck

public void print(){ //Begin Print Method

if(myBad != null){ //Begin If Statement (if the error variable is NOT null)
System.out.println(myBad); //Print the String Variable
} //End If Statement

else{ //Begin Else Statement
System.out.println("$" + myDollar + "." + myCent); //Print the text
System.out.println(myDollar + " dollars and " + myCent + " centsn"); //Print the text
} //End Else Statement

} //End Print Method

} //End Class DKSaleCheck






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 23:48







Dillon

















asked Nov 21 '18 at 23:38









DillonDillon

64




64













  • You have myBad += where you need myBad =. As it is initially null, you get a null.

    – user207421
    Nov 21 '18 at 23:41











  • But if I remove the "+" it will no longer find multiples. It ends after printing only the error with cents. For instance if I enter "$12d.de" it only shows the error in the cents part and not the dollar part.

    – Dillon
    Nov 21 '18 at 23:43













  • Please, format the code. On debug you can see what exactly is null in your case.

    – Sergei Sirik
    Nov 21 '18 at 23:44






  • 1





    One solution is to change myBad = null; to myBad = "";

    – Scary Wombat
    Nov 22 '18 at 0:17











  • What multiples? You can only get one exception at a time.

    – user207421
    Nov 22 '18 at 9:15





















  • You have myBad += where you need myBad =. As it is initially null, you get a null.

    – user207421
    Nov 21 '18 at 23:41











  • But if I remove the "+" it will no longer find multiples. It ends after printing only the error with cents. For instance if I enter "$12d.de" it only shows the error in the cents part and not the dollar part.

    – Dillon
    Nov 21 '18 at 23:43













  • Please, format the code. On debug you can see what exactly is null in your case.

    – Sergei Sirik
    Nov 21 '18 at 23:44






  • 1





    One solution is to change myBad = null; to myBad = "";

    – Scary Wombat
    Nov 22 '18 at 0:17











  • What multiples? You can only get one exception at a time.

    – user207421
    Nov 22 '18 at 9:15



















You have myBad += where you need myBad =. As it is initially null, you get a null.

– user207421
Nov 21 '18 at 23:41





You have myBad += where you need myBad =. As it is initially null, you get a null.

– user207421
Nov 21 '18 at 23:41













But if I remove the "+" it will no longer find multiples. It ends after printing only the error with cents. For instance if I enter "$12d.de" it only shows the error in the cents part and not the dollar part.

– Dillon
Nov 21 '18 at 23:43







But if I remove the "+" it will no longer find multiples. It ends after printing only the error with cents. For instance if I enter "$12d.de" it only shows the error in the cents part and not the dollar part.

– Dillon
Nov 21 '18 at 23:43















Please, format the code. On debug you can see what exactly is null in your case.

– Sergei Sirik
Nov 21 '18 at 23:44





Please, format the code. On debug you can see what exactly is null in your case.

– Sergei Sirik
Nov 21 '18 at 23:44




1




1





One solution is to change myBad = null; to myBad = "";

– Scary Wombat
Nov 22 '18 at 0:17





One solution is to change myBad = null; to myBad = "";

– Scary Wombat
Nov 22 '18 at 0:17













What multiples? You can only get one exception at a time.

– user207421
Nov 22 '18 at 9:15







What multiples? You can only get one exception at a time.

– user207421
Nov 22 '18 at 9:15














1 Answer
1






active

oldest

votes


















0














Scary Wombat had the best answer. I changed:



public DKSaleCheck(String mySale) {    
myBad = null;
//rest of class
}

public void print(){
if(myBad != null)
//rest of class
}


to:



public DKSaleCheck(String mySale) {    
myBad = "";
//rest of class
}

public void print(){
if(myBad != "")
//rest of class
}


Worked like a champ!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421954%2fjava-random-null-popping-up-during-a-try-catch%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Scary Wombat had the best answer. I changed:



    public DKSaleCheck(String mySale) {    
    myBad = null;
    //rest of class
    }

    public void print(){
    if(myBad != null)
    //rest of class
    }


    to:



    public DKSaleCheck(String mySale) {    
    myBad = "";
    //rest of class
    }

    public void print(){
    if(myBad != "")
    //rest of class
    }


    Worked like a champ!






    share|improve this answer




























      0














      Scary Wombat had the best answer. I changed:



      public DKSaleCheck(String mySale) {    
      myBad = null;
      //rest of class
      }

      public void print(){
      if(myBad != null)
      //rest of class
      }


      to:



      public DKSaleCheck(String mySale) {    
      myBad = "";
      //rest of class
      }

      public void print(){
      if(myBad != "")
      //rest of class
      }


      Worked like a champ!






      share|improve this answer


























        0












        0








        0







        Scary Wombat had the best answer. I changed:



        public DKSaleCheck(String mySale) {    
        myBad = null;
        //rest of class
        }

        public void print(){
        if(myBad != null)
        //rest of class
        }


        to:



        public DKSaleCheck(String mySale) {    
        myBad = "";
        //rest of class
        }

        public void print(){
        if(myBad != "")
        //rest of class
        }


        Worked like a champ!






        share|improve this answer













        Scary Wombat had the best answer. I changed:



        public DKSaleCheck(String mySale) {    
        myBad = null;
        //rest of class
        }

        public void print(){
        if(myBad != null)
        //rest of class
        }


        to:



        public DKSaleCheck(String mySale) {    
        myBad = "";
        //rest of class
        }

        public void print(){
        if(myBad != "")
        //rest of class
        }


        Worked like a champ!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 21:15









        DillonDillon

        64




        64
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421954%2fjava-random-null-popping-up-during-a-try-catch%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]