0

I'm new to java, and I've been trying to produce code reversing a string using recursion. I've spent about a day thinking of solutions as to why it's not working as expected, to no avail. At this point in my debugging, the code compiles, but when executing, it produces a "Exception in thread "main" java.lang.NullPointerException" error. Can you tell me what's causing this?

class RevStr {
  String str,Final;

  RevStr(String string) {
    String str=string;
    String Final="";
  }

  void Reverse() {
    String str=str;
    int count=1;

    if(str.length()==1) {
      Final+=str;
    }
    else {
      String last=str.substring(str.length()-1);
      Final+=last;
      str=str.substring(0,(str.length()-1));
      Reverse();
    }
  }

  String RetRev() {
    return Final;
  }
}

class ReverseString {
  public static void main(String args[]) {
    RevStr name=new RevStr("Cameron");
    name.Reverse();
    String rev=name.RetRev();
    //System.out.println(rev);
  }
}
  • 1
    What IDE do you use? IntelliJ has a nice debugger but any would do. Have you looked into **debuggubg** using a proper tool and not system.out? – Jason Mar 08 '20 at 20:17
  • I don't use an IDE, sticking to typing everything out in notepad. – CamoTheSlav Mar 08 '20 at 20:19
  • in RevStr you're not actually assigning the class member #str to the parameter. – Jason Mar 08 '20 at 20:19
  • 3
    Error #1: "I don't use an IDE". Once you fix that error, life will be much simpler for you. – Andreas Mar 08 '20 at 20:20
  • `String str=string;` **shadows** the field `str`. And thus `str` (the field) is `null`. Start by changing that to `this.str = string;` – Elliott Frisch Mar 08 '20 at 20:20
  • `RevStr(String string) { String str=string; String Final=""; }` you are declaring here *local variables* with name `str` and `Final`. Those are separate than fields with same name so those fields are still uninitialized. Also `String str=str;` shouldn't compile here so you would get different error than NPE. – Pshemo Mar 08 '20 at 20:20
  • lol @andreas if i could send u all my rep i would give it all to u for that comment. offtopic i know, sorry XD. – Jason Mar 08 '20 at 20:21
  • I had an answer, don't close this bad boy. – Jason Mar 08 '20 at 20:23
  • @Jason Is your answer better (more detailed, easier to understand, etc.) than answers provided in duplicate? If yes I can reopen it, but maybe instead consider posting your answer in duplicate question :) – Pshemo Mar 08 '20 at 20:25
  • Yeah u right :p – Jason Mar 08 '20 at 20:26
  • Thank you all, I finally got it (with your help)! I'll definitely invest in an IDE, and remember to use this. in these cases – CamoTheSlav Mar 08 '20 at 20:27
  • @CamoTheSlav make sure the variable that holds the result isn't null at first too, initialize as "" (an empty String) or you will get nullnoremaC – Jason Mar 08 '20 at 20:28
  • *I'll definitely invest in an IDE* @CamoTheSlav IntelliJ Community Edition, Eclipse and NetBeans are all free. All you must invest is the time to learn the tool. And learning the tool will yield enormous immediate benefit. – Elliott Frisch Mar 08 '20 at 20:35
  • @CamoTheSlav BlueJ is also free, easy to use, and has a debugger. – NomadMaker Mar 08 '20 at 20:37
  • The key to making a recursive function is to divide the problem up so that each call to the function works on a smaller problem. – NomadMaker Mar 08 '20 at 20:41

0 Answers0