I have written a piece of code, everything is fine until i run it, i get

"Exception in thread "main" java.lang.NullPointerException
	at useCADSL.main(useCADSL.java:5)"

i know what it means, i just cant see where to modify [probably the whole you read what you think you've wrote thing]

my first class is

import jp.ac.kobe_u.cs.cream.*;

public class IntFutoSol {
    public IntFutoSol with = this;
    Network net = new Network();
    int n = 4;
    IntVariable[][] v = new IntVariable[n][n];
    IntVariable[] vs = new IntVariable[n];
    String a,b,c,d,e,f,g,h,i,j,k,l,m,nn,o,p = null;
    
    public IntFutoSol() {
      net = new Network();
    }

    public static IntFutoSol CryptArith() {
      return null;
    }
    
    public IntFutoSol a(String a1) {
      a = a1;
      return this;
    }
    
    public IntFutoSol b(String b1) {
      b = b1;
      return this;
    }
    
    public IntFutoSol c(String c1) {
      c = c1;
      return this;
    }
    
    public IntFutoSol d(String d1) {
      d = d1;
      return this;
    }
    
    public IntFutoSol e(String e1) {
      e = e1;
      return this;
    }
    
    public IntFutoSol f(String f1) {
      f = f1;
      return this;
    }
    
    public IntFutoSol g(String g1) {
      g = g1;
      return this;
    }
    
    public IntFutoSol h(String h1) {
      h = h1;
      return this;
    }
    
    public IntFutoSol i(String i1) {
      i = i1;
      return this;
    }
    
    public IntFutoSol j(String j1) {
      j = j1;
      return this;
    }
    
    public IntFutoSol k(String k1) {
      k = k1;
      return this;
    }
    
    public IntFutoSol l(String l1) {
      l = l1;
      return this;
    }
    
    public IntFutoSol m(String m1) {
      m = m1;
      return this;
    }
    
    public IntFutoSol nn(String nn1) {
      nn = nn1;
      return this;
    }
    
    public IntFutoSol o(String o1) {
      o = o1;
      return this;
    }
    
    public IntFutoSol p(String p1) {
      p = p1;
      return this;
    }
  
    int z = Integer.parseInt(a);
    int y = Integer.parseInt(b);
    int x = Integer.parseInt(c);
    int w = Integer.parseInt(d);
    int vv = Integer.parseInt(e);
    int u = Integer.parseInt(f);
    int t = Integer.parseInt(g);
    int s = Integer.parseInt(h);
    int r = Integer.parseInt(i);
    int q = Integer.parseInt(j);
    int aa = Integer.parseInt(k);
    int bb = Integer.parseInt(l);
    int cc = Integer.parseInt(m);
    int dd = Integer.parseInt(nn);
    int ee = Integer.parseInt(o);
    int ff = Integer.parseInt(p);
    
    int[][] puzzle =
    {{z,y,x,w},
     {vv,u,t,s},
     {r,q,aa,bb},
     {cc,dd,ee,ff}};{
    
 // Create constraint-variables
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (puzzle[i][j] == 0){
        // A zero in the matrix represents and empty cell
          v[i][j] = new IntVariable(net, 1, n);
      }else
        v[i][j] = new IntVariable(net, puzzle[i][j]);
  }
  }
  
//Fixed-value constraints
    /*v[0][0].equals(z);
    v[0][1].equals(y);
    v[0][2].equals(x);
    v[0][3].equals(w);
    //System.out.printf("\n\n %s %s %s %s\n", z, y, x, w);
    v[1][0].equals(vv);
    v[1][1].equals(u);
    v[1][2].equals(t);
    v[1][3].equals(s);
    //System.out.printf("%s %s %s %s\n", vv, u, t, s);
    v[2][0].equals(r);
    v[2][1].equals(q);
    v[2][2].equals(aa);
    v[2][3].equals(bb);
    //System.out.printf("%s %s %s %s\n", r, q, aa, bb);
    v[3][0].equals(cc);
    v[3][1].equals(dd);
    v[3][2].equals(ee);
    v[3][3].equals(ff);
    //System.out.printf("%s %s %s %s\n", cc, dd, ee, ff);
    v[0][1].equals(2);
    v[0][3].equals(4);
    v[1][2].equals(3);
    v[2][1].equals(3);
    v[3][0].equals(4);  
    v[3][3].ge(v[2][3]);
  
  // Equation
  //v[1][1].ge(v[0][1]);
  //v[3][3].ge(v[3][2]);
  //v[2][3].ge(v[2][2]);
  //v[0][3].ge(v[0][2]);
  //v[0][1].ge(v[1][3]);
  //v[3][2].ge(v[3][1]);*/ 
  
 // Each row must contain distinct values
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      vs[j] = v[i][j];
    }
    new NotEquals(net, vs);
  }
  
//Each column must contain distinct values
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < n; i++) {
      vs[i] = v[i][j];
    }
    new NotEquals(net, vs);
  }

//Each box must contain distinct values
  for (int i0 = 0; i0 < n; i0 += 2) {
    for (int j0 = 0; j0 < n; j0 += 2) {
      int k = 0;
      for (int i = i0; i < i0+2; i++) {
        for (int j = j0; j < j0+2; j++) {
          vs[k++] = v[i][j];
        }
      }
      new NotEquals(net, vs);
    }
  }
  
  Solver solver = new DefaultSolver(net);
  
  for (solver.start(); solver.waitNext(); solver.resume()) {
    Solution solution = solver.getSolution();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        System.out.print(solution.getIntValue(v[i][j]) + " ");
      System.out.println();
    }
    System.out.println();
  }
  solver.stop();
}
 
}

and the second

public class useCADSL {
    public static void main(String[] args) {
  
  IntFutoSol.CryptArith()
      .a("0")
      .b("0")
      .c("0")
      .d("0")
      .e("0")
      .f("0")
      .g("0")
      .h("0")
      .i("0")
      .j("0")
      .k("0")
      .l("0")
      .m("0")
      .nn("0")
      .o("0")
      .p("0");
    }
}

Recommended Answers

All 4 Replies

The CryptArith() method in IntFutoSol returns a null, so attempting to use that result for .a("0") results in an NPE
Since I can't see from your uncommented code what you are trying to so, I can't tell you how to fix it.

As i thought, yes i know to do it, just in my mind i had haha
i have changed it to

public static IntFutoSol CryptArith() {
      return new IntFutoSol();
    }

I now get another one

Exception in thread "main" java.lang.NumberFormatException: null
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at IntFutoSol.<init>(IntFutoSol.java:99)
	at IntFutoSol.CryptArith(IntFutoSol.java:16)
	at useCADSL.main(useCADSL.java:4)

p.s. i am writing a 'Sudoku' solver, so that i enter the numbers a.("0") or a.("2")
and it will solve it

From line 99 onwards you declare a stack of variables and initialise them using tthe values of other variables (eg "a") that have been initialised to null, so you get an NPE when an instance of the class is initialised.
One line 120 the cunningly-concealed second { means that all the following code is interpreted as an instance initialiser - which means it compiles OK, but I have to suspect that's not what you intended.
I recommend a proper review of what code is in which method, and when should it be called.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.