简体   繁体   English

java.lang.NullPointerException: lock == null 来自 InputStreamReader

[英]java.lang.NullPointerException: lock == null from InputStreamReader

So I'm trying to parse an.obj wavefront file to be displayed with OpenGL ES, thing is, I'm getting the Nullpointer as if the file did not exist or was empty (?).所以我试图解析一个 .obj 波前文件以使用 OpenGL ES 显示,事情是,我得到 Nullpointer 就好像文件不存在或为空(?)。 I tried two different ways of getting to parse the file, also made sure there were no empty lines on it, put it in different folders (assets, src root, res, etc...) but the result is the same.我尝试了两种不同的方法来解析文件,还确保上面没有空行,将它放在不同的文件夹中(资产、src 根、res 等),但结果是一样的。 Maybe the error I'm getting is more to do with the OpenGL part of the code?也许我得到的错误更多地与代码的 OpenGL 部分有关? But I'm kinda lost, because apparently it should work... Also tried buffering the file outside the function, same happened.但我有点迷路了,因为显然它应该工作......还尝试在 function 之外缓冲文件,同样发生了。 From another question here, the problem the person had, had to do with " trying to update UI from worker Thread ".从这里的另一个问题来看,这个人遇到的问题与“尝试从工作线程更新 UI”有关。 Async did not help me here.异步在这里没有帮助我。

I got the code idea form this blog: http://etcodehome.blogspot.com/2011/07/android-rendering-3d-blender-models.html我从这个博客中得到了代码想法: http://etcodehome.blogspot.com/2011/07/android-rendering-3d-blender-models.html

And the file to base my work on from here: https://github.com/MartianIsMe/earth-live-wallpaper/blob/d71902aa642bad0c10fc46d6839ced6e15995f7b/%20earth-live-wallpaper/SLWP/src/com/seb/SLWP/DeathStar.java以及我的工作基础文件: https://github.com/MartianIsMe/earth-live-wallpaper/blob/d71902aa642bad0c10fc46d6839ced6e15995f7b/%20earth-live-wallpaper/SLWP/src/com/sathStar/SLWP/De java

 fun loadObjFile() {
    try {
        var str: String
        var tmp: Array<String>
        var ftmp: Array<String>
        var v: Float
        val vlist = ArrayList<Float>()
        val nlist = ArrayList<Float>()
        val fplist = ArrayList<Fp>()
        val mContext: Context? = null
        //val inb: BufferedReader = File("androidmodel.obj").bufferedReader()
        //val inputString = inb.use { it.readText() }
        val inb = BufferedReader(InputStreamReader(mContext?.getAssets()?.open
        ("src/main/res/androidmodel.obj")), 1024) //Error is here at com.example.xxx.MyGLRenderer.loadObjFile

        while (inb.readLine().also { str = it } != null) {
            tmp = str.split(" ".toRegex()).toTypedArray()
            //Parse the vertices
            if (tmp[0].equals("v", ignoreCase = true)) {
                for (i in 1..3) {
                    v = tmp[i].toFloat()
                    vlist.add(v)
                }
            }
            //Parse the vertex normals
            if (tmp[0].equals("vn", ignoreCase = true)) {
                for (i in 1..3) {
                    v = tmp[i].toFloat()
                    nlist.add(v)
                }
            }
            //Parse the faces/indices
            if (tmp[0].equals("f", ignoreCase = true)) {
                for (i in 1..3) {
                    ftmp = tmp[i].split("/".toRegex()).toTypedArray()
                    val chi = ftmp[0].toInt() - 1.toLong()
                    var cht = 0
                    if (ftmp[1] != "") cht = ftmp[1].toInt() - 1
                    val chn = ftmp[2].toInt() - 1
                    fplist.add(Fp(chi, cht, chn))
                }
                NBFACES++
            }
        }
        val vbb = ByteBuffer.allocateDirect(fplist.size * 4 * 3)
        vbb.order(ByteOrder.nativeOrder())
        mVertexBuffer = vbb.asFloatBuffer()
        val nbb = ByteBuffer.allocateDirect(fplist.size * 4 * 3)
        nbb.order(ByteOrder.nativeOrder())
        mNormBuffer = nbb.asFloatBuffer()
        for (j in fplist.indices) {
            mVertexBuffer?.put(vlist[(fplist[j].Vi * 3).toInt()])
            mVertexBuffer?.put(vlist[(fplist[j].Vi * 3 + 1).toInt()])
            mVertexBuffer?.put(vlist[(fplist[j].Vi * 3 + 2).toInt()])
            mNormBuffer?.put(nlist[fplist[j].Ni * 3])
            mNormBuffer?.put(nlist[fplist[j].Ni * 3 + 1])
            mNormBuffer?.put(nlist[fplist[j].Ni * 3 + 2])
        }
        mIndexBuffer = CharBuffer.allocate(fplist.size)
        for (j in fplist.indices) {
            mIndexBuffer?.put(j.toChar())
        }
        mVertexBuffer?.position(0)
        mNormBuffer?.position(0)
        mIndexBuffer?.position(0)
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

private class Fp
(var Vi: Long, var Ti: Int, var Ni: Int)

The problem is that you pass null into InputStreamReader .问题是您将null传递给InputStreamReader The path to the asset is wrong.资产路径错误。

  • First of all the file should be located under assets directory that is positioned on the same level in directory hierarchy as the java and res folder.首先,该文件应位于assets目录下,该目录与javares文件夹位于目录层次结构中的同一级别。
  • Second, you should pass path relative to the assets directory.其次,您应该传递相对于assets目录的路径。 So if your file is located directly under assets then the relative path is "androidmodel.obj" .因此,如果您的文件直接位于assets下,则相对路径为"androidmodel.obj" Thus, creating input stream will look like this:因此,创建输入 stream 将如下所示:
InputStreamReader(mContext?.getAssets()?.open("androidmodel.obj"))

But I strongly recommend you to check for non-null because if mContext is null - the issue will return.但我强烈建议您检查非空值,因为如果mContext是 null - 问题将返回。

mContext?.getAssets()?.open("androidmodel.obj")?.let { nonNullAsset ->
    InputStreamReader(nonNullAsset)
}

This part is crucial ?.let { as it runs the let function only if the object is not null.这部分是let?.let {

If there is no assets directory, just create it as a simple directory and it will be picked up by IDE automatically:如果没有assets目录,只需创建一个简单的目录,它会被 IDE 自动拾取:

主目录层次结构

Update更新

As the NPE still occurs the only reason left is the null value in mContext variable.由于 NPE 仍然发生,剩下的唯一原因是mContext变量中的null值。 Make sure it is initialized.确保它已初始化。

And after a little bit more digging, I can say that this was the issue from the beginning.经过更多的挖掘,我可以说这从一开始就是问题所在。 Any attempt to pass the wrong path of a file to the assets.open(fileName) function will result in FileNotFoundException .任何将文件的错误路径传递给assets.open(fileName) function 的尝试都将导致FileNotFoundException Thus, even though the path you use is wrong you did not even reach the point of opening a file as the context is null.因此,即使您使用的路径是错误的,您甚至没有达到打开文件的地步,因为上下文是 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM