Java中的动态变量初始化错误

时间:2017-06-29 06:46:41

标签: java

我有这段代码,提出错误说

  

变量priorityCheck未初始化

我需要的输出是查询在循环中以给定值priorityCheck执行。这是代码:

String query;
String StrComp;
int PerFound;
Statement stmt = null;
ResultSet rs = null;
String priorityCheck;

// running the queries
for (int i = 0; i < names.size(); i++) {
  StrComp = types.get(i).toString();
  PerFound = StrComp.indexOf("%");
  if (StrComp.indexOf("P1") != -1) {
    priorityCheck =
        "a.SubscriptionType=0 and a.applyticketpriorityfilterflag = 1 and a.P1 & a.P2 = 1";
  } else if (StrComp.indexOf("P2") != -1) {
    priorityCheck =
        "a.SubscriptionType=0 and a.applyticketpriorityfilterflag = 1 and a.P3 & a.P4 = 1";
  } else if (StrComp.indexOf("WO") != -1) {
    priorityCheck = "a.SubscriptionType=2";
  }

  if (PerFound == -1) {

    query =
        "SELECT DATEDIFF(minute,a.LastStatusDate,GETUTCDATE()) FROM SASubscriptionPIIView a,SAWorkflowToolPIIView b WHERE (a.toolid=b.id and a.active=1 and a.SubscriptionCategory=0 and "
            + priorityCheck
            + " and b.toolname like "
            + types.get(i)
            + ")";
    writeLog(query);
  } else {
    query =
        "SELECT DATEDIFF(minute,a.LastStatusDate,GETUTCDATE()) FROM SASubscriptionPIIView a,SAWorkflowToolPIIView b WHERE (a.toolid=b.id and a.active=1 and a.SubscriptionCategory=0 and "
            + priorityCheck
            + " and b.toolname like "
            + types.get(i)
            + ")";
    writeLog(query);
  }
}

2 个答案:

答案 0 :(得分:1)

此错误基本上意味着:您在代码中使用(读取!)变量,但代码中有路径初始化变量。

简化为:

String foo;
if (whatever) {
  foo = "from if";
}

bar = foo;

这就是你的代码正在做的事情:它包含一种方法来达到&#34; read&#34; priorityCheck没有先前的&#34;写&#34;到那个变量。一个简单的解决方法:

String foo = "not initialized";

换句话说:认为变量应包含的内容为&#34;默认&#34;。或者确保没有路径而不指定值。

除此之外:您的真正问题是您编写过于复杂的代码。如果你真的打算写#34;数据库代码&#34;在此刻;考虑阅读一下&#34;清洁编码实践&#34 ;;例如cv::Mat原则。您的问题是您编写的代码非常复杂,以至于您无法再查看代码正在执行的操作。

答案 1 :(得分:-1)

请使用默认值Module Directive{ class myDirectiveCtrl { private name: string; private items: Array<string>; private $q: ng.IQService; static $inject = ['$scope','$window','$q']; constructor($scope: ImyDirectiveScope, $window) { this.name = $scope.name; this.items = ['salut', 'hello', 'hi', 'good morning']; } clickMe = ():void => { console.log('dssff'); this.items.push('yo'); } } export interface ImyDirectiveScope { name: string } export class myDirective implements ng.IDirective { restrict = 'E'; template = '<div><h1>{{vm.name}}</h1><div ng-repeat="i track by $index in vm.items">{{i}}</div><hr/><button type="button" ng-click="vm.clickMe()">Click Me</button></div>'; replace = true; scope = { name: '@' }; controller = myDirectiveCtrl; controllerAs = 'vm'; link = (scope: ng.IScope, element: ng.IAugmentedJQuery, attributes: ng.IAttributes, controller: myDirectiveCtrl) => { }; constructor() { }; static factory(): ng.IDirectiveFactory { var directive = () => new myDirective(); return directive; } } app.directive('myDirective',Directive.myDirective.factory()); } 初始化变量priorityCheck。 问题在于代码

""

变量if (StrComp.indexOf("P1")!=-1) { priorityCheck="a.SubscriptionType=0 and a.applyticketpriorityfilterflag = 1 and a.P1 & a.P2 = 1"; } else if (StrComp.indexOf("P2")!=-1) { priorityCheck="a.SubscriptionType=0 and a.applyticketpriorityfilterflag = 1 and a.P3 & a.P4 = 1"; } else if (StrComp.indexOf("WO")!=-1) { priorityCheck="a.SubscriptionType=2"; } 无法初始化 - 其他语句丢失。