先谈谈我是怎么用的:

 DBCollection dbcollection =  XXXXXXXXXX();  //连接mongo
DBCursor dbCursor = mergeVideoDB.find(XXXX); //根据name查出若干个
if (dbCursor.length() == 1) {
return videoinfos;
}
while(dbcursor。hasNext()){ // 这一步产生错误,报出DBCursor的“can't switch cursor access methods”
XXXXXX;
     }
以上!

首先在使用hasNext方法后需要先通过 _checkType() 检查 cursor 类型。

 void _checkType( CursorType type ){
if ( _cursorType == null ){
_cursorType = type;
return;
} if ( type == _cursorType )
return; throw new IllegalArgumentException( "can't switch cursor access methods" );
}

回过头来看,  DBCursor dbCursor = mergeVideoDB.find(XXXX); //根据name查出若干个

此时,DBCursor的type为 null

 if (dbCursor.length() == 1) {
return videoinfos;
}

在执行完上述步骤后,DBCursor的type变为了array

所以在使用hasNext方法时会报出

"can't switch cursor access methods"
解决思路:
1.不要在之前使用会更改其type的方法进行操作;
2.使用除hasNext之外的其他遍历方法进行遍历
直接使用foreach方法进行遍历即可:
for (DBObject dbObject:dbCursor){}。
 

由DBCursor的“can't switch cursor access methods”异常引发的思考的更多相关文章

  1. 如何捕获access violation异常

    文章目录 access violation的由来 access violation的实例 Win32 exception SEH异常与C++标准异常 捕获方法 1.access violation的由 ...

  2. MongoDB ,cursor not found异常

    查询mongoDB集合数据更新,数据有400w多.我一次用cursor(游标)取1w,处理更新.程序在某段时间运行中遍历游标时发生异常! DBCursor cursor = tabColl.find( ...

  3. [改善Java代码]小心switch带来的空值异常

    使用枚举定义常量时,会伴有大量的switch语句判断,目的是伪类每个枚举项解释其行为,例如: public class Client { public static void main(String[ ...

  4. 提高你的Java代码质量吧:小心switch带来的空值异常

    一.分析  使用枚举定义常量时,会有伴有大量的switch语句判断,目的是为每个枚举解释其行为. 我们知道,目前的Java的switch语句只能判断byte.short.char.int类型(JDK7 ...

  5. js02--对象、函数、switch、for、异常、表单验证

    现在我们接着来继续学习有关js的一些基础. 1.undefined与null    undefined:当变量声明但尚未赋值时,它的类型就是undefined    null:表示一个不存在的对象,它 ...

  6. uvm Register Access Methods(16)

    转载: 译文:https://blog.csdn.net/zhajio/article/details/80731435 原文:http://cluelogic.com/2013/02/uvm-tut ...

  7. 集合并发修改异常-foreach的时候不可修改值

    直接上代码: 无意间发现的://这个方法本身是为后面的集合去掉前面集合的重复数据一直报错,并发修改异常,仔细看mainList正在迭代循环,然后我进行了remove操作,这个时候就会报这个错.故:总结 ...

  8. 为什么iterator,foreach遍历时不能进行remove操作?除了一种情况可以这样(特殊情况)?

    Exception in thread "main" java.util.ConcurrentModificationException 并发修改异常引发的思考! 1 foreac ...

  9. Heterogeneous Self-Organizing Network for Access and Backhaul

    This application discloses methods for creating self-organizing networks implemented on heterogeneou ...

随机推荐

  1. java_实现Hello World

    1.新建项目 在空白处右击--New--java Project 2.项目文件结构 新建了项目之后项目文件在工作空间里面,(如果忘记工作空间的路径可以点击File---Switch Workspace ...

  2. centos7下的/etc/rc.local自启动程序

    在centos6中有一个/etc/rc.local的启动文件,只要把需要经常启动的程序添加到此文件下并执行source /etc/rc.local就可以实现开机启动了. 在centos7中不知道也是如 ...

  3. c#读取文件夹路径,并保存在textBox1中

    private void button3_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.FolderBrowserDia ...

  4. WebRtc的一些基本概念

    GCC:Google Congestion Control,谷歌提出的拥塞控制算法 REMB:Receiver Estimated Maximum Bitrate,  接收端最大接收码率估测,接收端会 ...

  5. Mariadb多实例启动脚本

    #!/bin/bash port=3306 mysql_user="root" mysql_pwd="centos" cmd_path="/app/m ...

  6. MessageBox和ShellExecute初体验

    #include <stdio.h> //包含头文件,标准输入输出库 #include <windows.h> //包含windows头文件,ShellExecute正来自于此 ...

  7. Peter Shirley Ray Tracing in One Weekend(下篇)

    Peter Shirley-Ray Tracing in One Weekend (2016) 原著:Peter Shirley 下篇主要对本书的后5章节进行学习,包括材质球的Metal,和Diele ...

  8. Educational Codeforces Round 73 (Rated for Div. 2) C. Perfect Team

    链接: https://codeforces.com/contest/1221/problem/C 题意: You may have already known that a standard ICP ...

  9. SQL Server查询表结构语句

    --1:获取当前数据库中的所有用户表   www.2cto.com   select Name from sysobjects where xtype='u' and status>=0  -- ...

  10. springboot中使用spring security,登录url就出现403错误

    参考链接:https://segmentfault.com/q/1010000012743613 有两个controller,一个是所有用户可以访问的@RequestMapping("use ...