Dudebro Nation

Full Version: Can't call method "getAttribute" on an undefined value at ./
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6
line 114.

What the hell does that mean, and how do I fix it. I'm so close to my goal. Anyone that knows computer programming or anyone that know anyone that knows computer programming. NEED HELP.

The code was written in perl (not by me). Will post code next, highlighted is the problem area i believe
#!/usr/bin/perl

#################################################################
# Yahoo Weather Rss Information Atomizer
# Version 0.7.1
# Loud-Soft.com
# Provided As Is
#################################################################

use strict;
use XML::XPath;
use LWP::Simple;
use XML::XPath::XMLParser;
use Getopt::Long;
use File::Copy;

#################################################################
# Variables
#################################################################
# Constants (Change these to localize)
my $zipcode = "60642";
my $unit = "F";
my $scripthome = "/Library/prlprograms/yweather.pl";
my $icondir = $scripthome."images/";
my $datadir = $scripthome."data/";
my $datafile = $datadir."weather.xml";
my $imagefile = $icondir."weather.png";

# Constants (Do not change these)
my $pre="yweather";
my $uri="http://xml.weather.yahoo.com/ns/rss/1.0";
my $url="http://xml.weather.yahoo.com/forecastrss?p=$zipcode&u=$unit";
my %data;
my $xp;

#################################################################
# Subroutines
#################################################################
# Print usage
sub usage {
print "Yahoo Weather Information\n\n";
print "Usage:\n";
print " ./yweather.pl -ct Displays current temperature\n\n";
print "Arguments: \n";
print " -lc City \n";
print " -lr Region\n";
print " -lt Country\n";
print " -cc Weather Code (used for images)\n";
print " -ct Current Temperature\n";
print " -cw Current Weather Description\n";
print " -cd Current Date\n";
print " -ah Current Humidity\n";
print " -av Current Visibilty\n";
print " -ap Current Barometric Pressure\n";
print " -ar Change in Barometric Pressure\n";
print " -sr Time of Sunrise\n";
print " -ss Time of Sunset\n";
print " -wc Current Wind Chill\n";
print " -wd Current Wind Direction\n";
print " -ws Current Wind Speed\n";
print " -ut Temperature Unit\n";
print " -ud Distance Unit\n";
print " -up Pressure Unit \n";
print " -us Speed Unit\n";
print " -fd1 Tomorrow's Day\n";
print " -fg1 Tomorrow's Date\n";
print " -fl1 Tomorrow's Low Temp\n";
print " -fh1 Tomorrow's High Temp\n";
print " -ft1 Tomorrow's Description\n";
print " -fc1 Tomorrow's Weather Code\n";
print " -fd2 Day After Tomorrow's Day\n";
print " -fg2 Day After Tomorrow's Date\n";
print " -fl2 Day After Tomorrow's Low Temp\n";
print " -fh2 Day After Tomorrow's High Temp\n";
print " -ft2 Day After Tomorrow's Description\n";
print " -fc2 Day After Tomorrow's Weather Code\n";
print " --copyimage Copy Appropriate Image to Current Image\n";
print " --update Update xml source file\n" ;
print " \n";
print "All data is returned without units. To get data with units,\n";
print "use a combination of commands.\n\n";
print "Example: (Displays Current temperature with unit)\n";
print " ./yweather.pl -ct && ./yweather.pl -ut\n";
}

# Print data
sub args{
my ($arg) = @_;
print $data{$arg} . "\n";
}

# Subroutine to update xml data from yahoo
sub update_weather {
LWP::Simple::getstore($url,$datafile);
}

# Subroutine to download images from yahoo
sub get_images {
my $imgurl = "http://l.yimg.com/a/i/us/nws/weather/gr/";
for (0..47) {
LWP::Simple::getstore($imgurl.$_."d.png",$icondir.$_."d.png");
LWP::Simple::getstore($imgurl.$_."n.png",$icondir.$_."n.png");
}
File::Copy::copy($icondir."0d.png", $imagefile);
}

# Parse XML
sub get_data {
my($element, $attribute, $index) = @_;
if ($index){$index=1;}

my $nodeset = $xp->find("//yweather:$element");
my $node = $nodeset->get_node($index);
return $node->getAttribute($attribute);
}

# Copy correct image to the image define in $imagefile
sub copy_image {
my ($second, $minute, $hour, $dayOfMonth, $month,
$yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
my $night = $data{'ss'};
my $morning = $data{'sr'};
# my $condition = $data{'cc'};
my $imagesub;
if ($hour % 12){
if(($hour-12) < int(substr($night,0,1))){
$imagesub = "d";
}elsif(($minute) < int(substr($night,2,3))){
$imagesub = "d";
}else{
$imagesub = "n";
}
} else {
if(($hour) < int(substr($morning,0,1))){
$imagesub = "n";
}elsif(($minute) < int(substr($morning,2,3))){
$imagesub = "n";
}else{
$imagesub = "d";
}
}
File::Copy::copy($icondir.$data{'cc'}.$imagesub.".png", $imagefile)
or die "File ".$icondir.$data{'cc'}.$imagesub.".png"." cannot be copied. ".$data{'lr'};
}

#################################################################
# Check that files exist
#################################################################
#ensure directories exist
unless(-d $datadir){
mkdir $datadir;
}
unless(-d $icondir){
mkdir $icondir;
}

# Check if weather.xml exists
if (!(-e $datafile)){update_weather()}
$xp = XML::XPath->new(filename => $datafile);
$xp->set_namespace($pre, $uri);

# Check if images exist
if (!(-e $icondir."0d.png")){get_images()}

# Check if image exist
if (!(-e $imagefile)){copy_image()}


#################################################################
# Data Setup
#################################################################
# Location Information
$data{'lc'} = get_data("location","city");
$data{'lr'} = get_data("location","region");
$data{'lt'} = get_data("location","country");

# Current Weather Information
$data{'cc'} = get_data("condition","code");
$data{'ct'} = get_data("condition","temp");
$data{'cw'} = get_data("condition","text");
$data{'cd'} = get_data("condition","date");

# Current Atmosphere Information
$data{'ah'} = get_data("atmosphere","humidity");
$data{'av'} = get_data("atmosphere","visibility");
$data{'ap'} = get_data("atmosphere","pressure");
$data{'ar'} = get_data("atmosphere","rising");

# Todays Sunrise and sunset
$data{'sr'} = get_data("astronomy","sunrise");
$data{'ss'} = get_data("astronomy","sunset");

# Current wind information
$data{'wc'} = get_data("wind","chill");
$data{'wd'} = get_data("wind","direction");
$data{'ws'} = get_data("wind","speed");

# Unit information
$data{'ut'} = get_data("units","temperature");
$data{'ud'} = get_data("units","distance");
$data{'up'} = get_data("units","pressure");
$data{'us'} = get_data("units","speed");

# Forecast (Tomorrow)
$data{'fd1'} = get_data("forecast[1]","day");
$data{'fg1'} = get_data("forecast[1]","date");
$data{'fl1'} = get_data("forecast[1]","low");
$data{'fh1'} = get_data("forecast[1]","high");
$data{'ft1'} = get_data("forecast[1]","text");
$data{'fc1'} = get_data("forecast[1]","code");

# Forecast (Day after tomorrow)
$data{'fd2'} = get_data("forecast[2]","day");
$data{'fg2'} = get_data("forecast[2]","date");
$data{'fl2'} = get_data("forecast[2]","low");
$data{'fh2'} = get_data("forecast[2]","high");
$data{'ft2'} = get_data("forecast[2]","text");
$data{'fc2'} = get_data("forecast[2]","code");

#################################################################
# Parse arguments
#################################################################
if(($#ARGV + 1) == 1){
my $arg = substr($ARGV[0],1);
if ($data{$arg}){
args($arg);
} elsif($arg eq "-update"){
update_weather();
} elsif($arg eq "-copyimage"){
copy_image();
} else {
usage();
}
} else {
usage();
}
Found a slight variation in # Parse XML

sub get_data {
my $ret;
my($element, $attribute, $index) = @_;
if ($index){$index=1;}

my $nodeset = $xp->find("//yweather:$element");
my $node = $nodeset->get_node($index);
$ret = $node->getAttribute($attribute);
return $ret;
}
Wtf
i'm not a programmer, and dont know much about it, can you please explain what you just said

or are you just fucking with me?
Derick Wrote:Wtf
i'm not a programmer, and dont know much about it, can you please explain what you just said

or are you just fucking with me?
He's fucking with you
You need to re-write that section of code in your program. Whether that will help or not, idk.

If you google search "Yahoo Weather Rss Information Atomizer," you can find the perl program and download it

http://code.google.com/p/yweather/

yweather.pl......it's on the left side of the page.
Joe In PA Wrote:
Derick Wrote:Wtf
i'm not a programmer, and dont know much about it, can you please explain what you just said

or are you just fucking with me?
He's fucking with you

yep, I made it up :roll:
Brian in NY Wrote:You need to re-write that section of code in your program. Whether that will help or not, idk.

If you google search "Yahoo Weather Rss Information Atomizer," you can find the perl program and download it

http://code.google.com/p/yweather/

yweather.pl......it's on the left side of the page.
the code your looking at is yweather.pl

but there have been a couple of variations, i'm just not sure why it doesnt work. i keep getting this error relating to "getattribute".
brian might have been on to something. the code is slighly different.

Original
# Parse XML
sub get_data {
my($element, $attribute, $index) = @_;
if ($index){$index=1;}

my $nodeset = $xp->find("//yweather:$element");
my $node = $nodeset->get_node($index);
return $node->getAttribute($attribute);
}

Variation I found I the like brian posted
# Parse XML
sub get_data {
my $ret;
my($element, $attribute, $index) = @_;
if ($index){$index=1;}

my $nodeset = $xp->find("//yweather:$element");
my $node = $nodeset->get_node($index);
$ret = $node->getAttribute($attribute);
return $ret;
}



Comments anyone?
Wtf :tool: :killme: :fag:
:whomo:
stop hijacking my thread you :homo: 's
[Image: computer-cat-3.jpg]
[Image: 270909735_3d641143ff.jpg]
[Image: computer_cat-12532.jpg]
Pages: 1 2 3 4 5 6