Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 为什么我会有IndexOutOfBoundsException,而我的其他人应该阻止它?_Scala_Exception_Playframework - Fatal编程技术网

Scala 为什么我会有IndexOutOfBoundsException,而我的其他人应该阻止它?

Scala 为什么我会有IndexOutOfBoundsException,而我的其他人应该阻止它?,scala,exception,playframework,Scala,Exception,Playframework,我正在做一些测验,有一个问题和答案的列表,这些问题和答案通过控制器转移到我的视图类。人们可以在一个页面上提问和回答问题,然后我的系统开始“收集”这些问题,并对它们进行测验 如果您是第一个启动程序/测验的人,则问题列表为空。因此,我想用if/else子句检查一个空测验,if的情况似乎很好,但是else的情况抛出了一个IndexOutOfBoundsException,我不明白为什么。我认为当问题列表为空时,不会使用else部分,因此不应抛出异常。应该 查看类: @(questionList: Li

我正在做一些测验,有一个问题和答案的列表,这些问题和答案通过控制器转移到我的视图类。人们可以在一个页面上提问和回答问题,然后我的系统开始“收集”这些问题,并对它们进行测验

如果您是第一个启动程序/测验的人,则问题列表为空。因此,我想用if/else子句检查一个空测验,if的情况似乎很好,但是else的情况抛出了一个
IndexOutOfBoundsException
,我不明白为什么。我认为当问题列表为空时,不会使用else部分,因此不应抛出异常。应该

查看类:

@(questionList: List[Question], answerList: List[Answer], answerRadioForm: Form[Answer])

@if(questionList.length == 0){
    No questions yet!
}

else {
<!-- As only the highest ranked question gets put into the List, there is only one entry on first place -->
<b>@questionList.get(0).questionText</b>

    @for(question <- questionList)  {
        @question.questionText - @question.ownerID <br>
    }
} 
[IndexOutOfBoundsException: Index: 0, Size: 0]
49          <b>"""),_display_(/*27.8*/questionList/*27.20*/.get(0).questionText),format.raw/*27.40*/("""</b>
@if(questionList.length == 0){
    No questions yet!
} else {
@(问题列表:列表[问题]、回答列表:列表[答案]、回答无线电表格:表格[答案])
@如果(questionList.length==0){
还没有问题!
}
否则{
@问题列表。获取(0)。问题文本

@对于(问题首先,您的代码是否编译,因为列表中没有
get
方法。您可以使用
List.headOption

嗯,我可以使用
问题列表(0)

另一个解决方案

@questionList.headOption.map(q=>{q.text}).getOrElse(“还没有问题!”)

@对于(问题首先,您的代码是否编译,因为列表中没有
get
方法。您可以使用
List.headOption

嗯,我可以使用
问题列表(0)

另一个解决方案

@questionList.headOption.map(q=>{q.text}).getOrElse(“还没有问题!”)

@对于(问题我已经找到了一个解决方案,尽管回答你自己的问题是一种不好的做法,但我已经为此搜索了几个小时,也许我的答案对其他人有帮助:

if/else之间不能有返回/换行符

不起作用吗

@if(questionList.length == 0){
    No questions yet!
}

else { ...
有效:

@(questionList: List[Question], answerList: List[Answer], answerRadioForm: Form[Answer])

@if(questionList.length == 0){
    No questions yet!
}

else {
<!-- As only the highest ranked question gets put into the List, there is only one entry on first place -->
<b>@questionList.get(0).questionText</b>

    @for(question <- questionList)  {
        @question.questionText - @question.ownerID <br>
    }
} 
[IndexOutOfBoundsException: Index: 0, Size: 0]
49          <b>"""),_display_(/*27.8*/questionList/*27.20*/.get(0).questionText),format.raw/*27.40*/("""</b>
@if(questionList.length == 0){
    No questions yet!
} else {

编辑:如
@if(questionList.length>0){
也能工作,对意外插入新行比较稳定,而且更容易阅读和理解,我将使用此选项而不是其他选项。

我找到了一个解决方案,虽然回答自己的问题是一种不好的做法,但我已经搜索了几个小时,也许我的答案对其他人有帮助:

if/else之间不能有返回/换行符

不起作用吗

@if(questionList.length == 0){
    No questions yet!
}

else { ...
有效:

@(questionList: List[Question], answerList: List[Answer], answerRadioForm: Form[Answer])

@if(questionList.length == 0){
    No questions yet!
}

else {
<!-- As only the highest ranked question gets put into the List, there is only one entry on first place -->
<b>@questionList.get(0).questionText</b>

    @for(question <- questionList)  {
        @question.questionText - @question.ownerID <br>
    }
} 
[IndexOutOfBoundsException: Index: 0, Size: 0]
49          <b>"""),_display_(/*27.8*/questionList/*27.20*/.get(0).questionText),format.raw/*27.40*/("""</b>
@if(questionList.length == 0){
    No questions yet!
} else {

编辑:由于
@if(questionList.length>0){
也能工作,对意外插入换行符比较稳定,而且更容易阅读和理解,所以我将使用此选项而不是其他选项。

刚刚注意到:我可以使用
@if(questionList>0)…
然后我的代码工作了,但我不明白为什么其他人不做同样的事情。有什么想法吗?你的代码看起来应该工作-我想知道
==
操作符是否出现了假阴性,可能是由于类型?(即
questionList.length
与literal
0
的类型不同,因此它们并不相等)。一般来说,我更喜欢使用
@if(questionList.isEmpty)
或类似的方法来避免类似的问题。我用
@if(questionList.isEmpty)进行了测试
,没有更改。但在尝试此操作时,我找到了解决方案:if括号和else之间不能有返回/换行符…因此感谢您的提示!刚刚注意到:我可以使用
@if(questionList>0)…
然后我的代码工作了,但我不明白为什么其他人不做同样的事情。有什么想法吗?你的代码看起来应该工作-我想知道
==
操作符是否出现了假阴性,可能是由于类型?(即
questionList.length
与literal
0
的类型不同,因此它们并不相等)。一般来说,我更喜欢使用
@if(questionList.isEmpty)
或类似的方法来避免类似的问题。我用
@if(questionList.isEmpty)进行了测试
,无更改。但在尝试此操作时,我找到了解决方案:if括号和else之间不能有回车/换行符……因此,感谢您的提示!回答您自己的问题不是一个坏习惯。回答您自己的问题也不是一个坏习惯。我很惊讶,这对我不起作用。Scala和playframework的版本是什么你在使用?我在使用play framework 2.3.8。它有自己的scala,所以我不知道scala到底是哪个版本。你能发布你的代码吗?可能是在一个粘贴箱或类似的东西中?我很想看看不同版本中的代码是否不同。你在你的示例中使用的是哪一个版本的play framework?我很惊讶,不起作用g对我来说。你使用的是什么版本的Scala和playframework?我使用的是play framework 2.3.8。它有自己的Scala,所以我不知道Scala到底是哪个版本。你能发布你的代码吗?可能是在一个粘贴箱或类似的东西中?看看在不同的版本中这是否不同会很有趣。play framework的哪个版本是ar你在例子中使用了什么?