This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
EveryOS's profile picture

What is a null pointer exception?

Started by EveryOS, 07 June 2018 - 01:10 AM
EveryOS #1
Posted 07 June 2018 - 03:10 AM
NOTICE: This is on an older version of CC than I normally use!

EDIT: it seems to happen whenever I interact with the variable loc, but I do not know why
Here is the code (with some debug stuff):

local function import(class)
	if not classes[class] then
		class = string.gsub(class, "%.", "/")
  print("X") --Shows this X
  loc = loc --errors here
  print("X") --Never shown
		class = (loc or "").."/"..class..".lua"
  local cc = getFile(class)
  if not cc then
   error("File missing", -1)
  end
		local myclass = load(cc, OSName..":"..class, "tb", env)
		classes[class] = myclass()
	end
	return classes[class]
end

GitHub is not up to date, so it will not be of much use
Edited on 07 June 2018 - 01:33 AM
Bomb Bloke #2
Posted 07 June 2018 - 04:34 AM
NOTICE: This is on an older version of CC than I normally use!

It'd be faster and significantly more useful to post the an actual version number. How on earth is anyone supposed to know what version you usually use, let alone figure out what version you're using now?!

And the error is easily researched, too!

If you meant to ask why you're getting it, then I doubt there's enough information here to say - I'm sure you don't get this error if you call this function in a vacuum, and you've neglected to provide the rest of your code. But why are you performing "loc = loc" anyway? Is that purely for testing purposes, or did you mean to create a local version instead?
EveryOS #3
Posted 07 June 2018 - 04:44 AM
Testing purposes, was it CC1.74 or CC1.75, I can't remember?

If you remove that part of code, it will error later here:

class = (loc or "").."/"..class..".lua"

By "what is it" I actually meant "why am I getting it in computercraft", because I'm not working with java, so I should not get that error, the JVM LuaJ should give me a more useful error

I think that this code might be useful to know:

-snip-
env = CL.env
do
	local menv = env
	menv._G = menv
	menv.classes = {}
	menv.env = env
        menv.loc = loc
	import = load(string.dump(import), OSName..":Importer", "t", menv)
end
-snip-
Edited on 07 June 2018 - 02:48 AM
valithor #4
Posted 07 June 2018 - 05:01 AM
In ask a pro it is extremely important to provide all pieces of relevant information such as in this case every single variable declaration used in the function. Without these details it is absolutely impossible for us to help. As squiddev said in one of your previous ask a pro threads there is almost never a use for string.dump. And this definitely is not one of the times it should be used.

string.dump has limitations whenever you use it such as:
"Converts a function f into binary representation, which can be subsequently processed by loadstring to retrieve the function. The function must be a Lua function without upvalues." (see link)

I didn't take the time to test anything once i saw how string.dump was being used. If you want to use load on a function store the function as text in a variable and just load it that way.

link: https://www.gammon.c...lua=string.dump
Edited on 07 June 2018 - 03:02 AM
Luca_S #5
Posted 07 June 2018 - 05:21 AM
If I see this correctly you even have your code on GitHub. Why not link to that and tell us the specific file the error occurs in? Also you should probably read the sections titled "Posting a Good Question" and "Get Help Faster" from here.
Edited on 07 June 2018 - 03:21 AM
SquidDev #6
Posted 07 June 2018 - 07:59 AM
By "what is it" I actually meant "why am I getting it in computercraft", because I'm not working with java, so I should not get that error, the JVM LuaJ should give me a more useful error
It probably shouldn't be giving you an error at all, but LuaJ has a few bugs which means it errors when it shouldn't, or doesn't provide as useful error messages as it should.

To echo valithor, stop using string.dump - it makes things harder to debug and increases the risk of hitting erroneous cases inside LuaJ. If the error persists, try to narrow down the issue to as small a bit of code as possible, then post it here - this makes it much easier to track down the problem.
EveryOS #7
Posted 07 June 2018 - 08:04 PM
But otherwise I don't get the syntax highlighting on my editor if I put it is a string. In addition, I am staying away from setfenv - It's deprecated

If I see this correctly you even have your code on GitHub.
I said that:
GitHub is not up to date, so it will not be of much use
Very bottom of my post
Edited on 07 June 2018 - 06:07 PM
SquidDev #8
Posted 07 June 2018 - 08:08 PM
But otherwise I don't get the syntax highlighting on my editor if I put it is a string. In addition, I am staying away from setfenv - It's deprecated

You're better off reading the file's contents and passing it into load directly - there's very rarely a reason to use string.dump.
You don't have to use loadfile - it's perfectly OK to roll your own.
EveryOS #9
Posted 07 June 2018 - 08:14 PM
I didn't use loadfile this time - It was a function called getfile. But I wanted to wrap import because I need to be able to change the class table later on with a different one, and I don't want to pass the string between multiple function calls
valithor #10
Posted 07 June 2018 - 11:07 PM
But otherwise I don't get the syntax highlighting on my editor if I put it is a string. In addition, I am staying away from setfenv - It's deprecated

You can either do this correctly and have it work, or have syntax highlighting. There are a couple of solutions to this.

I didn't use loadfile this time - It was a function called getfile. But I wanted to wrap import because I need to be able to change the class table later on with a different one, and I don't want to pass the string between multiple function calls

Both of the following solutions do not require you to pass a string between multiple function calls, all they require is a extra variable or file.

Solution 1
Create a local string variable in your program something like importString and change the "import = load(string.dump(import), OSName..":Importer", "t", menv)" to "import = load(importString, OSName..":Importer", "t", menv)" you would also want to define import as a local variable whenever you define importString.

Solution 2
Copy the function into a another file and read all of the files contents in and treat what you read in as importString from solution 1.

Either way string.dump is not intended to be used how you are using it, and deciding that you won't do it the correct way because you want syntax highlighting is just bad practice.
Edited on 07 June 2018 - 09:09 PM