Java:无法获得价值

时间:2016-03-17 22:33:04

标签: java

常数类:

SELECT 
    ddTask_id,
    dd_id,
    SUM(EstimatedCost) AS EstimateCost
FROM 
(
    SELECT vJoboutsEst.ddTask_id, vJoboutsEst.dd_id,SUM(vJoboutsEst.jo_cost) as EstimatedCost
    FROM vJoboutsEst WHERE vJoboutsEst.jo_type = 0 GROUP BY vJoboutsEst.ddTask_id, vJoboutsEst.dd_id

    UNION ALL

    SELECT vMaterialsEst.ddTask_id, vMaterialsEst.dd_id,SUM(vMaterialsEst.item_amt) as EstimatedCost
    FROM vMaterialsEst WHERE vMaterialsEst.material_type = 0 GROUP BY vMaterialsEst.ddTask_id, vMaterialsEst.dd_id

    UNION ALL


    SELECT vLaborsEst.ddTask_id, vLaborsEst.dd_id,SUM(vLaborsEst.labor_amt) as EstimatedCost
    FROM vLaborsEst WHERE vLaborsEst.labor_type = 0 GROUP BY vLaborsEst.ddTask_id, vLaborsEst.dd_id
)aa
GROUP BY
    ddTask_id, dd_id

FileHandle类:

public class Constants { 
    FileHandle h = new FileHandle();
    String[] LIST_DATA3 = h.getA().split(Pattern.quote("."));
    public static  int NEW_ELEMENT_ID =0;
}

错误:

public class FileHandle {

   String a;
   String b;
   String c;
   String d;

  public void openFile() throws FileNotFoundException {        
    File dir = new File("DB");
    if (!dir.exists()){
    dir.mkdirs();
    }
   // System.out.println(dir.getAbsolutePath());
    if (dir.isDirectory()) {
        for (File file : dir.listFiles()) {
            try (Scanner s = new Scanner(file)) {
                if (s.hasNext()) {
                    this.a = (a == null) ? s.next() :a + "."+s.next();                        
                    b = (b == null) ? s.next() :b + "." + s.next();
                    c = (c == null) ? s.next() :c + "." + s.next();
                    d = (d == null) ? s.next() :d + "." + s.next();

                }else{
                s.close();
                }
            }  
        }System.out.println(a); 

    }
  }
  public String getA(){
      if(this.a !=null){
      System.out.println(this.a);
      }

   return this.a;
}
}

第63帧:

Exception in thread "main" java.lang.NullPointerException
at databasesearch.Constants.<init>(Constants.java:16)
at databasesearch.Frame.CreatingGUI(Frame.java:63)
at databasesearch.DatabaseSearch.main(DatabaseSearch.java:23)
C:\Users\D1sturbance\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

我的问题(在我看来,告诉我,如果我错了):获取NPE,因为常量假设getA()返回非null - 它甚至没有调用openFile(),所以它是's&#39; s在这种情况下总是会为null。我该如何解决?

1 个答案:

答案 0 :(得分:0)

在您的DatabaseSearch类中写下:

String aaaa;
public DatabaseSearch(){

FileHandle h = new FileHandle();
    try {    
        h.openFile();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(DatabaseSearch.class.getName()).log(Level.SEVERE, null, ex);
    }
 pirmas = h.a;
 System.out.println(pirmas+"cia");
}