Jump to content




What is a null pointer exception?


9 replies to this topic

#1 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 07 June 2018 - 02:10 AM

NOTICE: This is on an older version of CC than I normally use!Posted Image

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 by EveryOS, 07 June 2018 - 02:33 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 June 2018 - 03:34 AM

View PostEveryOS, on 07 June 2018 - 02:10 AM, said:

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?

#3 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 07 June 2018 - 03: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 by EveryOS, 07 June 2018 - 03:48 AM.


#4 valithor

  • Members
  • 1,053 posts

Posted 07 June 2018 - 04: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 by valithor, 07 June 2018 - 04:02 AM.


#5 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 07 June 2018 - 04: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 by Luca_S, 07 June 2018 - 04:21 AM.


#6 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 07 June 2018 - 06:59 AM

View PostEveryOS, on 07 June 2018 - 03:44 AM, said:

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.

#7 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 07 June 2018 - 07: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

View PostLuca_S, on 07 June 2018 - 04:21 AM, said:

If I see this correctly you even have your code on GitHub.
I said that:

View PostEveryOS, on 07 June 2018 - 02:10 AM, said:

GitHub is not up to date, so it will not be of much use
Very bottom of my post

Edited by EveryOS, 07 June 2018 - 07:07 PM.


#8 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 07 June 2018 - 07:08 PM

View PostEveryOS, on 07 June 2018 - 07:04 PM, said:

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

View PostSquidDev, on 31 May 2018 - 05:59 PM, said:

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.

#9 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 07 June 2018 - 07: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

#10 valithor

  • Members
  • 1,053 posts

Posted 07 June 2018 - 10:07 PM

View PostEveryOS, on 07 June 2018 - 07:04 PM, said:

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.

View PostEveryOS, on 07 June 2018 - 07:14 PM, said:

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 by valithor, 07 June 2018 - 10:09 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users