-1

I've got a question to my program:

public class Muster2 {
int[] farben;

constructs an array with 4 integers

public Muster2(int f0, int f1, int f2, int f3) {
int[] farben = new int[4];
farben[0] = f0;
farben[1] = f1;
farben[2] = f2;
farben[3] = f3;
}

casts this array into a String

public String toString() {
String result = "";
    for (int i=0;i<4;i++) {
      char convert = (char) farben[i];
      result+=convert;
    }
return result;
}

The Idea: At first, I want to see where the two Arrays have the same integer at the same position: Example: [1,2,3,4] and [1,4,5,6]. The counter_weiss should be = 1 for those two arrays, cause only the element at position 0 is the same for both arrays. So counter_weiss counts the elements, which are the same at exactly the same position in both arrays. The counter_schwarz counts the elements, which exist in both arrays, but on other positions. So counter_schwarz would be = 1 too, for the given arrays, cause in the first we have 4 at position 3 and in the second array there we can find the 4 at the second position.

public Bewertung bewerte(Muster versuch) {
int counter_schwarz = 0;  
int counter_weiss = 0;

They have to be 0 at first

for (int i=0;i<4;i++) { 

I run through the array

if (this.farben[i] != versuch.farben[i]) {

and check if I got the same elements in the same position

for (int o=i+1;o<4;i++) {      

if not, i check if i have it on another position, but i have to start with o at i+1, cause otherwise I would count things twice when I get to an index at the end of the array like i=3. Lets say 0,3 is a possible combination, so the first element of the first array has the same number like the second on position 3. Then counter_schwarz would get +1 from 0,3 and also from 3,0.

if(this.farben[i] == versuch.farben[o]) {   
              counter_schwarz += 1;                            
       }
    }
 }
 else counter_weiss +=1;   
 }

if the elements at the same position are the same, counter_white gets +1

Bewertung aktuelleBewertung = new Bewertung (counter_schwarz, counter_weiss);

return aktuelleBewertung;  /* here the counters are used to construct an object, 
which doesn't matter now, this part should work fine */
}

public static void main (String[] args) {

Muster Muster1 = new Muster (1,2,3,4);
Muster Muster2 = new Muster (2,3,4,5);

Bewertung Neu = Muster1.bewerte(Muster2);

System.out.println(Neu.toString());

}

}

The Problem now is, that I get a NullPointerException at

if (this.farben[i] != versuch.farben[i])

I don't know why, can anybody help me?

Thanks

rikojir
  • 115
  • 2
  • 13
  • 3
    I'm confused: You state that `"Equal Method in my class for Objects doesn't work"`, but I don't see an `equals(...)` method anywhere in your code above. – Hovercraft Full Of Eels Jun 29 '13 at 20:11
  • 4
    you people even use german for variable names!? – Boris Mocialov Jun 29 '13 at 20:12
  • Whatever is the case, which I certainly don't understand, I've just one question - Why aren't you passing an array in your constructor, instead of 4 separate integer arguments to construct an array? – Rohit Jain Jun 29 '13 at 20:13
  • Where have you defined `Muster` ? – Vishal K Jun 29 '13 at 20:13
  • And please post Short and compilable program to demonstrate what's wrong. – Rohit Jain Jun 29 '13 at 20:14
  • 1
    @HovercraftFullOfEels has it spot on here... What equals()/hashCode() are we talking about here? – fge Jun 29 '13 at 20:16
  • Sorry, name of the topic was wrong, was the name of an old topic i wanted to post, but I found out what was the problem. The Problem is that I get a nullPointerException here. Muster is another class, that works fine, I can post it if you want. Yes, it is Mastermind^^ – rikojir Jun 29 '13 at 20:19

2 Answers2

1

The issue is that here:

public class Muster2 {
int[] farben;

and here:

public Muster2(int f0, int f1, int f2, int f3) {
int[] farben = new int[4];

you declare a two completely separate variables both called farben.

You populate the latter and let is go out of scope. The former remains null, so you get an NPE when you try to access its elements.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Okay, I've found the mistake. It is in another class, the toString()-method of Bewertung.java doesn't work, I'll ask this in a new thread. Thanks for all your help! :) – rikojir Jun 29 '13 at 20:44
  • @rikojir I hope you mean that you have found **another** mistake. As not fixing this mistake (in the answer here) would only cause more issues. – skiwi Jun 29 '13 at 20:56
0

If you use 'o' as loop counter, then you should not increment 'i':

for (int o=i+1;o<4;i++) {

Otherwise, it is very hard to track errors from other peoples programs if they dont post complete executable examples!

You should perhaps learn how to use a debugger (NetBeans or Eclipse make debugging rather painless), or sprinkle your code with lots of println()'s to see whats happening.

Gyro Gearless
  • 5,131
  • 3
  • 18
  • 14
  • And once you have this working, you should learn about StringBuffer. Avoid using += on Strings, this is an mistake in Java that this operator even exists. – Gyro Gearless Jun 29 '13 at 20:25
  • Okay, this really helped, it was the array initialized two times and the loop counter o – rikojir Jun 29 '13 at 20:31
  • @GyroGearless That hasn't been true about string concatenation in Java in quite a while. The compiler converts += into calls to a StringBuilder now, see for example the answer to this question: http://stackoverflow.com/questions/4648607/stringbuilder-stringbuffer-vs-operator Additionally this is just a loop with 4 steps so performance is not going to suffer regardless, might as well make the code short and readable. – confusopoly Jun 29 '13 at 20:32
  • But now I can compile it but it just gives out a ' and nothing else. – rikojir Jun 29 '13 at 20:32
  • So the method works now, but I dont have a clue why it doesn't give out the right things. The toString()-method should work, or is there a mistake? – rikojir Jun 29 '13 at 20:34
  • @confusopoly: It is well known that "+=" on Strings is really translated to use a StringBuilder by the java compiler. The problem with using "+=" in a loop is that for every loop cycle: 1) A new StringBuilder is allocated 2) The required append() operations are done on the StringBuilder and 3) toString() is used to create an intermediary String result. Please prove me wrong if recent Java compilers are clever enough to optimize += in a loop. – Gyro Gearless Jun 29 '13 at 21:06
  • @rikojir: Apart from that academic discussion on Strings (please forget that for now), your toString() looks OK. But i suppose you are confusing chars and ints: casting say integer 1 to char does not yield '1', instead you get an invisible control char (SOH in Ascii) – Gyro Gearless Jun 29 '13 at 21:11
  • @GyroGearless I concede that in a loop this optimization likely does not happen. But the loop in this case has only 4 steps, so I think concatenation is not a problem in the current case. – confusopoly Jun 29 '13 at 21:15