Note that there are some explanatory texts on larger screens.

plurals
  1. POJava NullPointerException on Simple Array for Chinese Studies
    primarykey
    data
    text
    <p>I am working on a very simple command line program in Java to test my knowledge of the Chinese writing system. The goal is to present English, Pinyin, or Hanzi and to then force the user to enter the 2 which are not shown. After working through a few simple Array errors, I encountered a NullPointerException.</p> <p>Now, I understand what it is, I simply can't see why it is occurring.</p> <p>In the file called Master.java, I have the following:</p> <pre><code>static final String [] [] Mandarray = {{"You", "Nǐ", "你"}, {"Good", "Hǎo", "好"}}; </code></pre> <p>In the constructor of my Chinese_Mandarin object, I copy the contents of this array to a new one, one of the members of the Chinese_Mandarin object, like so,</p> <pre><code> public Chinese_Mandarin(char transfer){ if(transfer == 'n' || transfer != 'y')System.exit(0); for (int i = (Master.Mandarray.length-1); i&gt;0 ;i--){ mandalist [i][0] = Master.Mandarray[i][0]; mandalist [i][1] = Master.Mandarray[i][1]; mandalist [i][2] = Master.Mandarray[i][2]; } </code></pre> <p>With the above 'if' part of the system that asks if the user wishes to continue, it is not important. Next, I check to make sure that the same entry isn't to be shown again by seeing if the first character is *, which, by my program, indicates the entry has already been used.</p> <pre><code>public void presenter(){ Scanner chinEngl = new Scanner(System.in); restart: for (int i = mandalist.length; i&gt;0 ;i--){ randInt = random.nextInt(mandalist.length); randIntSub = random.nextInt(3); if(mandalist[randInt][randIntSub] == ""){ continue restart; } else if (((mandalist[randInt][randIntSub]).charAt(0)) == '*'){ continue restart; } </code></pre> <p>Here, I initialize a scanner to be used later, create two random integers to be used later on, and make sure the elements aren't empty (for if they are, I pass to the tag 'restart' to continue looping despite this).</p> <p>On the line:</p> <pre><code>else if (((mandalist[randInt][randIntSub]).charAt(0)) == '*'){ </code></pre> <p>I receive a NullPointerException error, but cannot fathom why... The Master.mandarray has been transferred to mandalist, I can assume (but that's why I am here) and as such there should be no trouble retrieving the first char of a full entry, right?</p> <p>Is the problem that the system may retrieve a Chinese character and not know how to handle it? Or is something that I am missing afoot? Thank you to anyone who may help me. You will surely get credits!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload