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

[2 Problems] Java exeption thrown | os.loadAPI not working

Started by DjTranceFire, 17 February 2015 - 11:29 AM
DjTranceFire #1
Posted 17 February 2015 - 12:29 PM
Hey there.
I currently have 2 little problem within the same script so i think its not necessary to post 2 threads.
If thats not ok, or its better to post 2 threads just say something! :P/>

I'm building a large room to control mobspawner with CC.
My system should check the items inside of my ME system and if mobdrops are missing it should start the mobspawner.
The script in general is working fine except 2 little problems.
I'm using many variables and because of that i decided to throw the variables into a second script.
I'm loading the script with os.loadAPI and sometimes its working, sometimes not.

This is my main script (pastebin):
Spoiler

os.loadAPI("items")

local me = peripheral.find("tileinterface")
local m = peripheral.find("monitor")
local sSide = "right"

local function centerText(text, y)
	mX = m.getSize()
	x = math.floor(mX/2) - math.floor(string.len(text)/2)
	m.setCursorPos(x,y)
	m.write(text)
end

local function setupMonitor()
	m.clear()
	m.setTextColor(colors.white)
	m.setBackgroundColor(colors.black)
	centerText("Global Mob-Spawn System",1)
	m.setCursorPos(1,3)
	m.write("Mob:")
	m.setCursorPos(16,3)
	m.write("State:")
	m.setCursorPos(34,3)
	m.write("Color:")
	m.setCursorPos(1,4)
	m.write("--------------------------------------------------")
end

local function setOnline()
	rs.setBundledOutput(sSide, colors.combine(rs.getBundledOutput(sSide), curcolor))
	m.setCursorPos(16,curline)
	m.setBackgroundColor(colors.green)
	m.write(" ONLINE ")
	m.setBackgroundColor(colors.gray)
	m.write(" OFFLINE ")
end

local function setOffline()
	rs.setBundledOutput(sSide, colors.subtract(rs.getBundledOutput(sSide), curcolor))
	m.setCursorPos(16,curline)
	m.setBackgroundColor(colors.gray)
	m.write(" ONLINE ")
	m.setBackgroundColor(colors.red)
	m.write(" OFFLINE ")
end

local function updateList()
	local list = me.getAvailableItems()
	for num, item in pairs(list) do
		for n, i in pairs(mobdrops) do
			curline = i.line
			curcolor = i.color
			if i.mob == "Pech" then
				if item.fingerprint.nbt_hash == i.nbt then
					if item.size < i.qty then
						if i.nbt == "ad5cc1bd74d2230c5c483e4c1bbc7948" then ignis = "1"
						elseif i.nbt == "bb9211b894dea99a6b674ebe63759333" then terra = "1"
						elseif i.nbt == "64630f917da3a7214b1ef2c91a90d090" then aer = "1"
						elseif i.nbt == "358c318e194980b0c0835a9f1ce41750" then aqua  = "1"
						elseif i.nbt == "a77c4b35bbb1a4c84b15122dd52354bf" then ordo = "1"
						elseif i.nbt == "18add493aeeb97a3fe71cf8130fe8675" then perditio = "1" end
					elseif item.size >= i.qty then
						if i.nbt == "ad5cc1bd74d2230c5c483e4c1bbc7948" then ignis = "2"
						elseif i.nbt == "bb9211b894dea99a6b674ebe63759333" then terra = "2"
						elseif i.nbt == "64630f917da3a7214b1ef2c91a90d090" then aer = "2"
						elseif i.nbt == "358c318e194980b0c0835a9f1ce41750" then aqua  = "2"
						elseif i.nbt == "a77c4b35bbb1a4c84b15122dd52354bf" then ordo = "2"
						elseif i.nbt == "18add493aeeb97a3fe71cf8130fe8675" then perditio = "2" end
					end
				end
				if ignis == "2" and terra == "2" and aer == "2" and aqua == "2" and ordo == "2" and perditio == "2" then
					setOffline()
				else
					setOnline()
				end
			else
				if item.fingerprint.id == i.id and item.fingerprint.dmg == i.dmg and item.size < i.qty then
					setOnline()
				elseif item.fingerprint.id == i.id and item.fingerprint.dmg == i.dmg and item.size >= i.qty then
					setOffline()
				end
			end
			m.setBackgroundColor(colors.black)
			m.setCursorPos(1,i.line)
			m.write(i.mob)
			m.setCursorPos(36,i.line)
			m.setBackgroundColor(i.color)
			m.write("  ")
			m.setBackgroundColor(colors.black)
		end
	end
	sleep(1)
end

local function startScan()
	rs.setBundledOutput(sSide, 0)
	while true do
		updateList()
		sleep (1)
	end
end

setupMonitor()
startScan()

this is the script that holds all the different variables (pastebin):
Spoiler

ignis = "2"
terra = "2"
aer = "2"
aqua = "2"
ordo = "2"
perditio = "2"

mobdrops = {
	leather = {
		mob = "Horse",
		color = colors.red,
		id = "minecraft:leather",
		nbt = "",
		dmg = 0,
		qty = 64,
		line = 5
	},
	
	feather = {
		mob = "Chicken",
		color = colors.green,
		id = "minecraft:feather",
		nbt = "",
		dmg = 0,
		qty = 64,
		line = 6
	},
	
	blazerod = {
		mob = "Blaze",
		color = colors.brown,
		id = "minecraft:blaze_rod",
		nbt = "",
		dmg = 0,
		qty = 128,
		line = 7
	},
	
	witherskull = {
		mob = "WhiterSkeleton",
		color = colors.blue,
		id = "minecraft:skull",
		nbt = "",
		dmg = 1,
		qty = 30,
		line = 8
	},
	
	inksack = {
		mob = "Squid",
		color = colors.purple,
		id = "minecraft:dye",
		nbt = "",
		dmg = 0,
		qty = 128,
		line = 9
	},
	
	wool = {
		mob = "Sheep",
		color = colors.cyan,
		id = "minecraft:wool",
		nbt = "",
		dmg = 0,
		qty = 128,
		line = 10
	},
	
	bones = {
		mob = "Skeleton",
		color = colors.lightGray,
		id = "minecraft:bone",
		nbt = "",
		dmg = 0,
		qty = 128,
		line = 11
	},
	
	gunpowder = {
		mob = "Creeper",
		color = colors.gray,
		id = "minecraft:gunpowder",
		nbt = "",
		dmg = 0,
		qty = 256,
		line = 12
	},
	
	slimeball = {
		mob = "Slime",
		color = colors.pink,
		id = "minecraft:slime_ball",
		nbt = "",
		dmg = 0,
		qty = 128,
		line = 13
	},
	
	ghasttear = {
		mob = "Ghast",
		color = colors.lime,
		id = "minecraft:ghast_tear",
		nbt = "",
		dmg = 0,
		qty = 64,
		line = 14
	},
	
	pinkslimeball = {
		mob = "Pink Slime",
		color = colors.yellow,
		id = "MineFactoryReloaded:pinkslime",
		nbt = "",
		dmg = 0,
		qty = 72,
		line = 15
	},
	
	mana_ignis = {
		name = ignis,
		mob = "Pech",
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "ad5cc1bd74d2230c5c483e4c1bbc7948",
		dmg = 0,
		qty = 256,
		line = 16
	},
	
	mana_terra = {
		name = terra,
		mob = "Pech",
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "bb9211b894dea99a6b674ebe63759333",
		dmg = 0,
		qty = 256,
		line = 16
	},
	
	mana_aer = {
		name = aer,
		mob = "Pech",
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "64630f917da3a7214b1ef2c91a90d090",
		dmg = 0,
		qty = 256,
		line = 16
	},
	
	mana_aqua = {
		name = aqua,
		mob = "Pech",
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "358c318e194980b0c0835a9f1ce41750",
		dmg = 0,
		qty = 256,
		line = 16
	},
	
	mana_ordo = {
		name = ordo,
		mob = "Pech",
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "a77c4b35bbb1a4c84b15122dd52354bf",
		dmg = 0,
		qty = 256,
		line = 16
	},
	
	mana_perditio = {
		name = perditio,
		mob = "Pech",
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "18add493aeeb97a3fe71cf8130fe8675",
		dmg = 0,
		qty = 256,
		line = 16
	}
}

When i reboot the computer sometimes its loading just fine and starts the main script.
But sometimes its not loading the "items" and prints errors about missing tables blahblah…
Is there anything i did wrong or why does this happen?


The second problem is the bigger problem.
The script is working realy nice but sometimes it just crashes with


Java exception thrown

I will have no clue what "Java exception thrown" means.
And i have absolutly no clue why this happens.
There is no way to reproduce this.
Sometimes it happens after a few seconds and sometimes the script runs for hours without problems.
I realy hope someone here knows a solution for my problems.

Sorry for my bad english, i hope its not to hard to understand.
HPWebcamAble #2
Posted 17 February 2015 - 05:32 PM
I don't have access to my PC right now, so I can't really test anything, but…

Just by looking, I would recommend using the fs api to open the scripts file, then 'textutils.unserialize' to convert to a table, instead of using os.loadAPI
In that case, you would just move the items like ignis and terra into the main script, and leave the table (or have them IN the table)

You could also try just moving all the variables into the main script.

I believe the most common java error is when a function calls itself over 256 times, but I don't see a place here where that could happen.
valithor #3
Posted 17 February 2015 - 05:50 PM
The most common java error is actually when you try to use a peripheral incorrectly. When a function calls itself over 256 times is a different error.

I have never used any peripheral mod that has a peripheral named "tileinterface", but that is what I would think is causing it. My suggestion is to throw unique prints throughout the program, so you can narrow down exactly where the program is crashing. Java Exception errors unfortunately do not give a line number, and are thus more difficult to find than others.

As HP said it is better to put the variables into a file and use the fs api and textutils.unserialize to access them.
Bomb Bloke #4
Posted 17 February 2015 - 11:46 PM
Just by looking, I would recommend using the fs api to open the scripts file, then 'textutils.unserialize' to convert to a table, instead of using os.loadAPI
In that case, you would just move the items like ignis and terra into the main script, and leave the table (or have them IN the table)

Eg.